c++ - 移动构造函数是自动生成的吗?

标签 c++ c++11 move-constructor

我有一个很大的类(class),里面有很多 STL 容器。
编译器会自动创建一个移动构造函数,将这些容器移动到目标,还是我必须自己创建?

最佳答案

X 类的移动构造函数在何时被隐式声明为默认

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator,
  • X does not have a user-declared destructor, and
  • the move constructor would not be implicitly defined as deleted.

例如,如果你的类有一个没有移动构造函数的类类型数据成员,那么即使它没有声明任何复制/移动构造函数,你的类也不会得到移动构造函数,因为隐式声明move 构造函数将被定义为已删除(因为该数据成员)。

关于c++ - 移动构造函数是自动生成的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283589/

相关文章:

c++ - 复制/移动构造函数的奇怪行为以及如何返回大对象?

c++ - 没有 std::move 的右值引用

c++ - 为什么我的do case循环内的开关盒无法正常工作,即使应该继续运行也会中断循环?

c++ - MacOS 10.12 上的 getopt

c++ - 将 std::function 与 lambda 和可变参数模板一起使用

c++ - unique_ptr 是否保证在 move 后存储 nullptr?

c++ - 为什么变量用最大值而不是零初始化

c++ - 从指针访问 map 的索引

适用于 Windows XP SP1 的 C++11

c++ - 按值返回时强制 RVO/移动构造