考虑以下事项:
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 是否适用于对象成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!