mardi 17 avril 2018

Usage of move semantics in c++

I`m playing with move somantics in c++, and I creted small class to test it, but the behaviour is not as expected. On the line A c( foo() ) ; I expected a call of move constructor. Any idea what I am doing wrong and why the move constructor was not called?

class A
{
    public:
        A(){ std::cout << "constructor" << std::endl;};
        ~A(){ std::cout << "destructor" << std::endl;};
        A(const A& a){ std::cout << "copy constructor" << std::endl;};
        A(A&& a){ std::cout << "move constructor" << std::endl;};
        A& operator=(A&& a){ std::cout << "move assignment" << std::endl; return *this; };
        A& operator=(A& a){ std::cout << "copy assignment" << std::endl; return *this; };
};

A foo()
{
  A a;
  return a;  
};

int main()
{
   A a;
   A b = a;
   A c( foo() ) ; //expected move constructor here
}

output:

constructor
copy constructor
constructor
destructor
destructor
destructor

Aucun commentaire:

Enregistrer un commentaire