问题描述
考虑以下事项:
struct A { /* ... */ };
A foo() {
auto p = std::make_pair(A{}, 2);
// ... do something
return p.first;
}
auto a = foo();
p.first 会被复制、移动或 RVO 编辑吗?
Will p.first be copied, moved or RVO-ed?
推荐答案
我发现在 Visual Studio 2010 和 gcc-5.1 RVO 中未应用(参见例如 http://coliru.stacked-crooked.com/a/17666dd9e532da76).
I've found in Visual Studio 2010 and in gcc-5.1 RVO is not applied (see for example http://coliru.stacked-crooked.com/a/17666dd9e532da76).
标准的相关部分是12.8.31.1 [class.copy].它指出允许复制省略(我的突出显示):
The relevant section of the standard is 12.8.31.1 [class.copy]. It states that copy elision is permitted (my highlighting):
在具有类返回类型的函数中的return语句中,当表达式为非易失性自动对象的名称(除了函数参数或异常引入的变量-声明一个与函数返回类型具有相同类型(忽略cv限定)的处理程序([except.handle])),可以通过将自动对象直接构造到函数的返回值中来省略复制/移动操作
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
由于 p.first 不是对象的名称,因此禁止 RVO.
Since p.first is not the name of an object, RVO is prohibited.
这篇关于RVO 是否适用于对象成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)