Skip to content

Instantly share code, notes, and snippets.

@stefan-wolfsheimer
Created January 13, 2019 16:52
Show Gist options
  • Save stefan-wolfsheimer/82d9381e06366e088cc5b347ef3554cd to your computer and use it in GitHub Desktop.
Save stefan-wolfsheimer/82d9381e06366e088cc5b347ef3554cd to your computer and use it in GitHub Desktop.
C++ move semantics in a nutshell
#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