A constructor move is automatically generated, even a member does not have a move constructor?

Quote from C ++ Primer

if we explicitly ask the compiler to generate a move operation using = default, and the compiler cannot move all members, the move operation will be defined as remote

a move constructor is defined as remote if the class has a member that defines its own copy constructor but does not define a move constructor , or if the class has a member that does not define its own copy constructor and for which the compiler cannot synthesize the move

Some code seems to violate this rule:

#include <utility>
#include <iostream>

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

struct hasX {
    hasX() = default;
    hasX(const hasX &) = delete;
    hasX(hasX &&) = default;
    X mem;
};


int main()
{
    hasX hx, hx2 = std::move(hx);  //output is X(const X&)
}

X the move constructor does not define, and the compiler cannot synthesize it for it.

, hasX .

, hasX , hx2 = std::move(hx) "X(const X&)", hasX, X . , , .

, ++ ?

, : VS2015 -

!

+4
1

. - , const type&, rvalues, .

hasX(hasX &&) = default;

hasX(hasX &&rhs) : mem(std::move(rhs.mem)) {}

, .

+3

Source: https://habr.com/ru/post/1679832/


All Articles