c++ - 当 RVO/NRVO 启动时对象是否被复制?

标签 c++ copy-constructor nrvo rvo

由于多个问题,我无法理解 RVO(和 NRVO)的定义 like this one在我看来,假设 RVO 省略了复制构造函数。现在按照12.8.15

In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.

这看起来不是复制构造函数调用被省略,而是复制本身——只是对象首先在“复制”位置构造,因此没有“原始”对象,也根本没有复制。因此,即使类具有 private 复制构造函数,它也可以在 RVO 启动时从函数返回,因为没有复制。

我做对了吗?是复制本身被省略了还是复制构造函数调用被省略了?当对象类具有私有(private)复制构造函数时,是否应允许从函数返回对象?

最佳答案

如果优化生效, 将省略复制,但仍需要编译器检查复制构造函数是否可访问。否则,如果编译器(或其他编译器)决定不优化,代码将无效。

关于c++ - 当 RVO/NRVO 启动时对象是否被复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292646/

相关文章:

C++帮助复制构造函数

c++ - 在模板化成员函数的返回类型中使用 std::enable_if 时的编译器差异

c++ - Openfeint 导致了这个 : ISO C++ forbids of declaration 'myClass' with no type

c++ - 如何复制匿名 union

c++ - 复制语句如何识别其构造函数

c++ - C++ std::string 的 NRVO

C++ RVO : when it happens?

c++ - 获取指向 map 内部结构的指针

c++ - WTL 和 CContainedWindow 导致访问冲突

c++ - 禁用一个函数的返回值优化