Skip to content

Instantly share code, notes, and snippets.

@luca3m
Created August 28, 2015 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luca3m/f9458eb474b2d9a84b8b to your computer and use it in GitHub Desktop.
Save luca3m/f9458eb474b2d9a84b8b to your computer and use it in GitHub Desktop.
C++ Usage of copy/move constructors on argument passing
#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