mercredi 25 septembre 2019

Why copy constructor is called instead of move constructor in my code?

I'm trying to understand a move constructor and rvalue reference. So I tried this code on https://www.onlinegdb.com/online_c++_compiler. But the result confuses me.

#include <iostream>
#include <type_traits>


class A {
public:
  A() { std::cout << "Constructed" << std::endl; }
  A(const A& )= delete;
  A(A&&) { std::cout << "Move Constructed" << std::endl; }
};

int
main ()
{
  A&& a = A();
  A b = a; // error: use of deleted function ‘A::A(const A&)’
  //A b = static_cast<decltype(a)>(a); // This works, WTF?
  std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire