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 是否适用于对象成员?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01