C++ 08 - copy elision
Contents
copy elision
when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
当用一个临时对象初始化另一个对象的时候, 如果他们两个的 cv-unqualified type 相同, 并且临时对象没有和任何引用绑定, 那么此次 copy/move construction 是可以省略的:
1 | struct X { |
直接编译的结果:
1 | RECKZHANG-MB1:cpp_test reckful$ g++ main.cc -o main |
copy/move construction 都被省略.
加上 -fno-elide-constructors 编译选项:
1 | RECKZHANG-MB1:cpp_test reckful$ g++ main.cc -o main -fno-elide-constructors |
class object 的两次拷贝构造分别发生在:
函数返回值保存在一个临时变量, 构造这个临时变量的时候调用.
main 函数中的 x 变量构造的时候调用.
注意
为了简化问题, 我们假设的 gcc 版本是 4.1.2 而且不支持 C++0x 及后续版本提出的 move semantic.
在后续版本, 即使添加编译选项 -fno-elide-constructors 也会省略掉不必要掉 copy/move constructor.