显式默认的移动构造函数 [英] Explicitly defaulted move constructor

查看:345
本文介绍了显式默认的移动构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据c ++ 11标准,默认移动构造函数仅在以下情况下生成:

According to the c++11 standard a default move constructor is only generated if:


  • X没有用户声明复制构造函数和

  • X没有用户声明的复制赋值操作符,

  • X没有用户声明的移动赋值运算符,

  • X没有用户声明的析构函数,

  • 移动构造函数不会被隐式定义为已删除。

  • X does not have a user-declared copy constructor, and
  • 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.

我还可以明确地默认它吗?似乎在clang正常工作。例如:

Can I still explicitly default it? Seems to work correctly in clang. Like this for example:

class MyClass {
private:
  std::vector<int> ints;
public:
  MyClass(MyClass const& other) : ints(other.ints) {}
  MyClass(MyClass&& other) = default;
};


推荐答案

这个规则的动机是,构造函数不适用于你的类,那么机会是默认的移动构造函数不会工作(规则5,或任何我们在C ++ 11)。

The motivation for that rule is that if the default copy constructor doesn't work for your class, then chances are the default move constructor won't work either (rule of 5, or whatever we're up to in C++11). So yes, you can explicitly default it, on your honor as a programmer that it'll work.

在您的示例代码中,您可以删除复制构造函数,因为它可以与默认值相同。

In your example code you could instead remove the copy constructor, since it does the same as the default.

这篇关于显式默认的移动构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆