rvo

Why g++ does not enable RVO here?

拟墨画扇 提交于 2019-11-28 02:16:50
问题 Consider that TEST code: #include <iostream> using namespace std; class Klass { public: Klass() { cout << "Klass()" << endl; } Klass(const Klass& right) { cout << "Klass(const Klass& right)" << endl; } }; Klass create(Klass a) { cout << "create(Klass a)" << endl; return a; } int main() { const Klass result = create(Klass()); } Compiling with: g++ -O3 rvo.cpp -o rvo The output is: $ ./rvo Klass() create(Klass a) Klass(const Klass& right) I was expecting the compiler to use the RVO mechanism in

C++ Unified Assignment Operator move-semantics

亡梦爱人 提交于 2019-11-27 18:49:56
EDIT: solved see comments --don't know how to mark as solved with out an answer. After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment operators. #include <string> #include <vector> #include <iostream> struct my_type { my_type(std::string name_) : name(name_) {} my_type(const my_type&)=default; my_type(my_type&& other) { this->swap(other); } my_type &operator=(my_type other) { swap(other); return *this; } void swap(my_type &other) { name.swap(other.name); } private: std::string name;

Why is RVO disallowed when returning a parameter?

丶灬走出姿态 提交于 2019-11-27 13:11:29
It's stated in [C++11: 12.8/31] : This elision of copy/move operations, called copy elision, is permitted [...] : — 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 or catch-clause parameter ) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value This implies #include <iostream> using namespace std; struct X { X() { } X(const X& other) { cout << "X(const X& other)" <

C++ Unified Assignment Operator move-semantics

ぐ巨炮叔叔 提交于 2019-11-27 04:19:20
问题 EDIT: solved see comments --don't know how to mark as solved with out an answer. After watching a Channel 9 video on Perfect Forwarding / Move semantics in c++0x i was some what led into believing this was a good way to write the new assignment operators. #include <string> #include <vector> #include <iostream> struct my_type { my_type(std::string name_) : name(name_) {} my_type(const my_type&)=default; my_type(my_type&& other) { this->swap(other); } my_type &operator=(my_type other) { swap

Why does Visual Studio not perform return value optimization (RVO) in this case

五迷三道 提交于 2019-11-27 01:01:42
问题 I was answering a question and recommending return by-value for a large type because I was confident the compiler would perform return-value optimization (RVO). But then it was pointed out to me that Visual Studio 2013 was not performing RVO on my code. I've found a question here regarding Visual Studio failing to perform RVO but in that case the conclusion seemed to be that if it really matters Visual Studio will perform RVO. In my case it does matter, it makes a significant impact to

Why is RVO disallowed when returning a parameter?

蹲街弑〆低调 提交于 2019-11-26 16:18:17
问题 It's stated in [C++11: 12.8/31] : This elision of copy/move operations, called copy elision, is permitted [...] : — 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 or catch-clause parameter ) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value This implies

Why does std::move prevent RVO?

╄→гoц情女王★ 提交于 2019-11-26 07:28:56
问题 In many cases when returning a local from a function, RVO kicks in. However, I thought that explicitly using std::move would at least enforce moving when RVO does not happen, but that RVO is still applied when possible. However, it seems that this is not the case. #include \"iostream\" class HeavyWeight { public: HeavyWeight() { std::cout << \"ctor\" << std::endl; } HeavyWeight(const HeavyWeight& other) { std::cout << \"copy\" << std::endl; } HeavyWeight(HeavyWeight&& other) { std::cout << \