Does a reference declaration introduce a new name for the referent?

淺唱寂寞╮ 提交于 2020-01-01 01:59:08

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/33371684/does-a-reference-declaration-introduce-a-new-name-for-the-referent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!