copy elison & RVO & NRVO

时间:2023-03-09 02:40:58
copy elison & RVO & NRVO

蓝色的博文

To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast, which also instructs the compiler that it's eligible to move the object. The price of moving is lower than copying but higher than RVO, so never apply std::move to local objects if they would otherwise be eligible for the RVO.

因此,在能够使用copy elision时,我们不要在return时加std::move()。在copy elision不work时,我们还是要加上std::move()从而调用move constructor而不是调用copy constructor.

博文中比较费解的是最后一个示例,这么写似乎产生了bug。

个人理解:最后一个示例触发了RVO,导致第一次拷贝:栈内变量拷贝至返回值临时变量被省略(两次拷贝参见下文),第二次拷贝:临时变量拷贝至具名变量未省略。而因为变量类型是右值引用,第二次的拷贝变成了移动构造。于是就出现了奇怪的输出。

Copy elision

In general, the C++ standard allows a compiler to perform any optimization, provided the resulting executable exhibits the same observable behaviour as if (i.e. pretending) all the requirements of the standard have been fulfilled. This is commonly referred to as the "as-if rule".[8] The term return value optimization refers to a special clause in the C++ standard that goes even further than the "as-if" rule: an implementation may omit a copy operation resulting from a return statement, even if the copy constructor has side effects.[1]

The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor has a visible side effect (printing text).[1] The first copy that may be eliminated is the one where a nameless temporary C could be copied into the function f's return value. The second copy that may be eliminated is the copy of the temporary object returned by f to obj.

#include <iostream>

struct C {
C() {}
C(const C&) { std::cout << "A copy was made.\n"; }
}; C f() {
return C();
} int main() {
std::cout << "Hello World!\n";
C obj = f();
return ;
}

Depending upon the compiler, and that compiler's settings, the resulting program may display any of the following outputs:

Hello World!
A copy was made.
A copy was made.
Hello World!
A copy was made.
Hello World!

这里有一篇博文,结合了《深度探索C++对象模型》,博文链接

在g++中开启选项-fno-elide-constructors可以去掉任何返回值优化,则C obj = f(); 中,会发生两次拷贝,f()内栈内变量拷贝构造返回值临时变量,返回值临时变量拷贝构造obj变量。

这里有一份更详细点的文档...

https://en.cppreference.com/w/cpp/language/copy_elision

===============================================================

最后是关于push_back与emplace_back的测试。

T &&var1 = std::move(var2); 不存在移动拷贝。

template< class... Args >
void emplace_back( Args&&... args );通过std::forward<Args>(args)...实现。
 #include <bits/stdc++.h>
using namespace std;
struct Stu {
int age;
Stu(const int age = ):age(age) {
cout << "construct" << endl;
}
Stu(const Stu& s){
cout << "copy construct" << endl;
}
Stu(Stu&& s) {
cout << "move construct" << endl;
};
Stu& operator = (const Stu& s) {
cout << "operator construct" << endl;
}
~Stu(){
cout << "destruct" << endl;
}
}; Stu Init(const int age) {
return Stu(age);
} int main() {
Stu s1();
cout << "1--------------" << endl;
Stu s2 = Init();
cout << "2--------------" << endl;
vector<Stu> ve;
ve.reserve();
cout << "3--------------" << endl;
ve.push_back(s1);
cout << "4--------------" << endl;
Stu&& ss = std::move(s1);
ve.push_back(std::move(s1));
cout << "5--------------" << endl;
ve.emplace_back(Stu());
cout << "6--------------" << endl;
ve.emplace_back();
return ;
} /*
construct
1--------------
construct
move construct
destruct
move construct
destruct
2--------------
3--------------
copy construct
4--------------
move construct
5--------------
construct
move construct
destruct
6--------------
construct
destruct
destruct
destruct
destruct
destruct
destruct
*/