This is my class and my program:
class A { public: int x; int* v; A() : x(5), v(new int[5]) {cout<<"default ctor. "; for (int i=0; i<x; i++) v[i]=rand()%10;} A(const A& a) : x(a.x), v(new int[x]) {cout<<"copy ctor. "; for (int i=0; i<x; i++) v[i]=a.v[i]; } A(A&& a) : x(a.x), v(a.v) {cout<<"move ctor. ";a.x=0;a.v=0;} A& f() {return *this;} A g() {return *this;} }; int main() { srand(time(0)); cout<<"A a --> ";A a;cout<<endl; cout<<"A b(A()) --> ";A b(A());cout<<endl; cout<<"A c(a.f()) --> ";A c(a.f());cout<<endl; cout<<"A d(a.g()) --> ";A d(a.g());cout<<endl; cout<<"A e(A().g()) --> ";A e(A().g());cout<<endl; } I would expect the objects b, d and e to use the move constructor, however the output I get is:
A a --> default ctor.
A b(A()) -->
A c(a.f()) --> copy ctor.
A d(a.g()) --> copy ctor.
A e(A().g()) --> default ctor. copy ctor.
If in all three cases r-values are the arguments of the constructors, why isn't the move constructor being used?
Obviously I am compiling with the -std=c++11 option.