Created
January 13, 2019 16:52
-
-
Save stefan-wolfsheimer/82d9381e06366e088cc5b347ef3554cd to your computer and use it in GitHub Desktop.
C++ move semantics in a nutshell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class A | |
{ | |
public: | |
A(const string & a) : value(a) | |
{ | |
cout << "A(const string & a)" << endl; | |
} | |
A(string && a) : value(move(a)) | |
{ | |
cout << "A(string && a)" << endl; | |
} | |
string value; | |
}; | |
string get_string() | |
{ | |
return "value"; | |
} | |
int main(int argc, const char ** argv) | |
{ | |
{ | |
string value("value"); | |
cout << "A a(value):" <<endl; | |
A a(value); | |
cout << a.value << endl << endl; | |
} | |
{ | |
cout << "A a(get_string()):" <<endl; | |
A a(get_string()); | |
cout << a.value << endl << endl; | |
} | |
{ | |
string value("value"); | |
cout << "A a(move(value)):" <<endl; | |
A a(move(value)); | |
cout << a.value << endl << endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment