C + + return value optimization and named return value optimization are compiler optimizations that improve performance in most cases, but are difficult to control by programmers. C++11 adds support for move semantics, which can have an impact on Rvo and Nrvo. This is illustrated in a section of code below.
Rvo and Nrvo in Copy/move Construct,copy/move assignment Eight simple cases, the test conditions are g++ 4.8.2 and clang++ 3.4, the default optimization.
#include <iostream>#include <vector>#include <string>structTest {test () {STD::cout<<"Construct a Test object"<<STD:: Endl; } Test (Consttest&) {STD::cout<<"Copy construct a Test object"<<STD:: Endl; } test&operator=(Consttest&) {STD::cout<<"Copy Assignment a Test object"<<STD:: Endl;return* This; } Test (test&&) {STD::cout<<"Move construct a Test object"<<STD:: Endl; }/* test& operator= (Test &&t) {std::cout << "move assignment a Test object" << St D::endl; return *this; } */~test () {STD::cout<<"Destruct a Test object"<<STD:: Endl; }}; Test Gettest () {returnTest ();} Test Gettestwithname () {test temp;returntemp;}intMain () {STD::cout<<"=============rvo=============="<<STD:: Endl;STD::cout<<"++test obj rvo for copy construct"<<STD:: Endl;AutoObj1 = Gettest ();STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test obj Rvo for move construct"<<STD:: Endl;Autoobj111 =STD:: Move (Gettest ());STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test obj rvo for copy assignment"<<STD:: Endl; Test Obj11; OBJ11 = Gettest ();STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test object Rvo for Move assignment"<<STD:: Endl; Test obj1111; obj1111 =STD:: Move (Gettest ());STD::cout<<"=============nrvo=============="<<STD:: Endl;STD::cout<<"++test obj nrvo for copy construct"<<STD:: Endl;AutoObj2 = Gettestwithname ();STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test obj Nrvo for move construct"<<STD:: Endl;Autoobj222 =STD:: Move (Gettestwithname ());STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test obj nrvo for copy assignment"<<STD:: Endl; Test obj22; obj22 = Gettestwithname ();STD::cout<<"--------------"<<STD:: Endl;STD::cout<<"++test obj Nrvo for move Assignment"<<STD:: Endl; Test obj2222; obj2222 =STD:: Move (Gettestwithname ());STD::cout<<"=============================="<<STD:: Endl;//std::string S1 = "S1 string move Semantics test", S2; //std::cout << "++before move s1\t" << s1 << Std::endl; //s2 = Std::move (S1); //std::cout << "++after move s1\t" << s1 << Std::endl; //std::cout << "=============" << Std::endl; return 0;}
Test results:
=============rvo==============++test obj RVO for copy constructconstruct a Test object--------------++test obj Rvo for move constructconstruct a test objectmove construct a test objectdestruct a Test object--------------++test obj rvo for copy assignmentconstruct a test objectconstruct a test objectmove assignment a test objectdestruct a Test object--------------++test object Rvo for Move assignmentconstruct a test objectconstruct a test objectmove assignment a test objectdestruct a Test object=============nrvo==============++test obj NRVO for copy constructconstruct a Test object--------------++test obj Nrvo for move constructconstruct a test objectmove construct a test objectdestruct a Test object--------------++test obj nrvo for copy assignmentconstruct a test objectconstruct a test objectmove assignment a test objectdestruct a Test object--------------++test obj Nrvo for move assignmentconstruct a test objectconstruct a test objectmove assignment a test objectdestruct a Test object==============================Destruct a test objectdestruct a test objectdestruct a test objectdestruct a test objectdestruct a test objectdestruct a T EST objectdestruct a test objectdestruct a test object
Several simple conclusions can be drawn from this:
1.copy construct itself in the Rvo and Nrvo two cases are optimized, if plus move instead of Lily.
2. After adding move assignment, the default is to call move assignment instead of copy assignment, and you can test the move assignment the comment.
3. For Rvo and Nrvo, the situation of construction compiler optimization is better, add move semantics mainly for the assignment has a relatively large impact
The impact of C + + Rvo/nrvo and move semantics