I have some simple questions regarding C++ move constructor, but so far didn't find a post that explains specifically what I saw below...
#include <utility>
class A
{
public:
A() {}
A(const A &) { std::cout << "copy Ctor" << std::endl; }
A(A&&) { std::cout << "move Ctor" << std::endl; }
};
int main()
{
A a;
A b(std::move(a));
}
The output is
move Ctor
If I explicitly delete the move constructor,
// A(A&&) { std::cout << "move Ctor" << std::endl; }
A(A&&) = delete;
I got compilation error,
error: use of deleted function ‘A::A(const A&&)’
so far so good.
Then I read that move constructor is not auto-generated when there is user-defined copy ctors, and I thought instead of explicit deletion, 'not declare it' should give the same result,
// A(A&&) { std::cout << "move Ctor" << std::endl; }
// A(A&&) = delete;
but then I got this output,
copy Ctor
Now I am confused,
- When I omit the declaration of move ctor, does
A b(std::move(a))
calls copy ctor directly? Is that part of the C++ standard? For regular functions this seems true - when a function's parameter is a xvalue, the function taking rvalue reference is preferred, and fallback is the function taking const lvalue reference. - But, if 'move constructor is not auto-generated when there is user-defined copy ctors', why 'explicit deletion' vs 'not declaring' makes a difference?