c – 是一个私人移动构造函数来防止移动?

前端之家收集整理的这篇文章主要介绍了c – 是一个私人移动构造函数来防止移动?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
C中的常见模式是使副本构造函数为private:
  1. class A
  2. {
  3. public:
  4. // ...
  5. private:
  6. A(const A&);
  7. };

但是下面的代码将会编译(在C 11/14中):

  1. A f();
  2.  
  3. auto a = f();

该标准包含有关自动生成移动构造函数的信息.我既没有访问标准也没有实际生成移动构造函数的编译器.我的问题是:我必须写

  1. class A
  2. {
  3. public:
  4. // ...
  5. private:
  6. A(const A&);
  7. A(const A&&);
  8. };

以防止移动(和类似运算符)?

解决方法

But will the following code then compile (in C++11/14):

不,不会.用户声明的复制构造函数的存在应该阻止移动构造函数的隐式生成.根据C11标准第12.8 / 9段:

If the definition of a class X does not explicitly declare a move constructor,one will be implicitly declared
as defaulted if and only if

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.

猜你在找的C&C++相关文章