C++ Primer 5th notes (chap 13 copy control) synthetic mobile operation

1. Appearance conditions

Only when a class does not define any copy control members of its own version, and every non-static data member of the class can be moved, the compiler will synthesize a constructor or move assignment operator for it.

struc X{
    
    
  int i;
  std::string s;
};

struc HasX{
    
    
  X mem;
};

X x, x2 = std::move(x);
HasX hx, hx2 = std::move(hx);

2. Description

If a member of the class is a class type, and the class has a corresponding move operation, the compiler can also move this member.

3. Conditions that do not appear

If a class has only a copy constructor, but no move constructor, in this case, the compiler will not synthesize the move constructor, and the matching rules of the function will ensure that objects of this type will be copied.

class Foo{
    
    
public:
    Foo() = default;
    Foo(const Foo &) ;//拷贝构造函数
    //其他成员的定义,但Foo为定义移动构造函数
};

Foo x;
Foo y(x);//拷贝构造函数x是一个左值
Foo z(std::move(x));//拷贝构造函数,因为未定义移动构造函数

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/113876089