- move assignment 的实现
- 三阶段:
1. Cleanup(释放当前资源)
2. Member-wise move
3. Reset(重置右边的值)
- 利用 `std::exchange` 简化操作
- 可选:处理 self move assignment
- 判断 this 相等
- 如果类存在自引用,考虑使用 Move-and-swap 方式比较安全
- 默认 move ctor 不一定是 noexcept 的
```cpp
struct Bar {
// 成员的不是 noexcept
Bar(Bar&&) {}
};
struct Foo {
Bar b;
// 不是 noexcept 的,因为 Bar 的 move ctor 不是
// 即 std::is_nothrow_move_constructible<Foo>::value 为 false
Foo(Foo&&) = default ;
};
```
- 如果自定义了 copy operations(copy ctor,copy assgin) 或者 destructor,则不会生成默认的 move operations。
- 例如自定义了 copy ctor,则不会自动生成默认的 move ctor 和 move assignment
- 使用 ` = default` 或者 ` = delete` ==也会==被认作是自定义。
> Core Guideline C.21:
> If you define or delete any copy, move, or destructor function, define or delete them all
- `std::move(f.a)` 与 `std::move(f).a` 的区别:
- 如果 a 是引用类型,后者是个左值
- https://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_member_access_operators