Does RVO work on object members?(RVO 是否适用于对象成员?)
问题描述
考虑以下事项:
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 是否适用于对象成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RVO 是否适用于对象成员?
基础教程推荐
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
