Created
August 28, 2015 10:35
-
-
Save luca3m/f9458eb474b2d9a84b8b to your computer and use it in GitHub Desktop.
C++ Usage of copy/move constructors on argument passing
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 <vector> | |
using namespace std; | |
class my | |
{ | |
public: | |
my() | |
{ | |
cout << "ctor" << endl; | |
} | |
my(int x) | |
{ | |
} | |
my(const my&) | |
{ | |
cout << "copy ctor" << endl; | |
} | |
my(my&&) | |
{ | |
cout << "move ctor" << endl; | |
} | |
void nonconst() | |
{} | |
}; | |
my r() | |
{ | |
my r; | |
return r; | |
} | |
class our | |
{ | |
public: our(my m): | |
m(move(m)){} | |
my m; | |
}; | |
class your | |
{ | |
public: | |
your(const my& m): | |
m(m){} | |
my m; | |
}; | |
class best | |
{ | |
public: | |
template<typename T> | |
best(T&& m): | |
m(forward<T>(m)){} | |
my m; | |
}; | |
template<typename T> | |
void log(T&& s) | |
{ | |
cout << s << endl; | |
} | |
int main() | |
{ | |
//my m; | |
// const vector<my> v { 1, 2 }; | |
log("my m;"); | |
my m; | |
log("our y(m);"); | |
our y(m); | |
log("our z(r());"); | |
our z(r()); | |
log(""); | |
log("your y(m);"); | |
your yy(m); | |
log("your zy(r());"); | |
your zy(r()); | |
log(""); | |
log("best b(m);"); | |
best b(m); | |
log("best b2(r());"); | |
best b2(r()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment