首页 > 解决方案 > 为什么默认析构函数会改变移动语义?

问题描述

#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.
}

在线演示

为什么默认析构函数会改变移动语义?

标签: c++c++11constructordestructormove-semantics

解决方案


推荐阅读