In this question we've learnt that RVO cannot be applied to an expression like p.first
.
In comments it was also suggested that RVO is generally not applied to an expression like r
after a declaration like auto& r = p.first
. It is less clear whether the standard mandates this behaviour.
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 parameter or a variable introduced by the exception-declaration of a handler ([except.handle])) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value
In the following code, is r
a name of the object also known as o
, to the extent that RVO is permissible when it forms the expression in a return
statement?
int o = 42;
int& r = o;
CWG #633 addressed the fact that references, unlike objects, didn't have actual names. It was resolved by N2993, which extended the notion of a variable to encompass references, thereby giving them names.
Now [basic]/6 reads (all emphasis by me):
A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable's name denotes the object or reference.
The name of a reference denotes that variable - the reference - not the object that the reference refers to. Although references are commonly explained as being "other names of objects/functions", in standard terms that definition is plain wrong.
I.e. copy elision is not applicable in your example.
Since the above paper was not adopted until 2009, and you tagged c++03: One can consider the paper as a retrospective correction of C++03. However, in C++03, strictly speaking, a reference is not an entity (this was rectified by CWG #485) and therefore the identifier in its declaration is never treated as a name (see [basic]/4, a name must denote a label or entity) - hence copy elision doesn't apply, again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With