Неявная обработка возвращаемого lvalue как rvalue

12.8 Копирование и перемещение объектов класса [class.copy] §31 и §32 говорят:

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

Следовательно, мы можем написать:

unique_ptr<int> make_answer()
{
    unique_ptr<int> result(new int(42));
    return result;   // lvalue is implicitly treated as rvalue
}

Однако я заметил, что g++ 4.6.3 также принимает lvalue, которые являются , а не именами , например:

    return (result);
    return *&result;
    return true ? result : result;

Напротив, return rand() ? result : result;не работает. Влияет ли оптимизатор компилятора на семантику языка? Как я интерпретирую стандарт, return (result);не должен компилироваться, потому что (result)— это не имя, а выражение в скобках. Я прав или не прав?

19
задан Xeo 16 July 2012 в 17:58
поделиться