3
#include <iostream>
#include <utility>

struct A
{
    A() = default;    
    A(A const&) { std::cout << "A(A const&)" << std::endl; }    
    A(A&&)      { std::cout << "A(A&&)" << std::endl; }
};

struct B { A a; ~B() = default; };    
struct C { A a; };

int main()
{
    auto b1 = B{};
    auto b2 = B(std::move(b1)); // A(A const&) is called.

    auto c1 = C{};
    auto c2 = C(std::move(c1)); // A(A&&) is called.
}

See online demo

Why does a default destructor change the move-semantics?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 2
    [Implicitly-declared_move_constructor](https://en.cppreference.com/w/cpp/language/move_constructor#Implicitly-declared_move_constructor) – Jarod42 Nov 15 '21 at 15:20
  • 1
    as to why it's to not break of C++98 programs. If you have an explicit destructor then most likely you need to do something special in the copy and move constructors. But since in C++98 there were no move constructors, a move constructor is not implicitly declared if you have a custom destructor. – bolov Nov 15 '21 at 15:21
  • 1
    It is to enforce [rule of 5/3/0](https://en.cppreference.com/w/cpp/language/rule_of_three). Ideally, should also apply to copy constructor/assignation, but that change cannot be done retro-actively. – Jarod42 Nov 15 '21 at 15:33

0 Answers0