a =默认移动构造函数等于成员式移动构造函数? [英] Does a =default move constructor equals to a member-wise move constructor?

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

问题描述

这是

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE):a {mE.a},b {mE.b} }
示例(示例&&&mE):a {move(mE.a)},b {move(mE.b)} {}
示例& operator =(const Example& mE){a = mE.a; b = mE.b; return * this; }
示例& operator =(Example&& mE){a = move(mE.a); b = move(mE.b); return * this; }
}

相当于

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE)
示例(示例&& mE)= default;
示例& operator =(const Example& mE)= default;
示例& operator =(Example&& mE)= default;
}




$ b $

  struct示例{
int a,b;
示例(int mA,int mB):a {mA},b {mB} {}
示例(const Example& mE)
示例(示例&& mE)= default;
示例& operator =(const Example& mE)= default;
示例& operator =(Example&& mE)= default;
}

此版本将允许您跳过正文定义。

$但是,当你声明显式默认函数时,你必须遵循一些规则:


8.4.2显式默认函数[dcl.fct.def.default]



一个形式的函数定义:

 属性指定声明声明符virt-speci- er-seqopt = 

称为显式默认定义。明确默认的函数




  • 是一个特殊的成员函数,


  • 具有相同的声明函数类型(除了可能不同的 ref-qualifiers ,除非在复制构造函数或复制赋值运算符的情况下,参数类型可以是非常量 T ,其中 T 是成员函数的类的名称) ,




Is this

struct Example { 
    int a, b; 
    Example(int mA, int mB) : a{mA}, b{mB}               { }
    Example(const Example& mE) : a{mE.a}, b{mE.b}        { }
    Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { }
    Example& operator=(const Example& mE) { a = mE.a; b = mE.b; return *this; } 
    Example& operator=(Example&& mE)      { a = move(mE.a); b = move(mE.b); return *this; } 
}

equivalent to this

struct Example { 
    int a, b; 
    Example(int mA, int mB) : a{mA}, b{mB} { }
    Example(const Example& mE)            = default;
    Example(Example&& mE)                 = default;
    Example& operator=(const Example& mE) = default;
    Example& operator=(Example&& mE)      = default;
}

?

解决方案

Yes both are the same.

But

struct Example { 
    int a, b; 
    Example(int mA, int mB) : a{mA}, b{mB} { }
    Example(const Example& mE)            = default;
    Example(Example&& mE)                 = default;
    Example& operator=(const Example& mE) = default;
    Example& operator=(Example&& mE)      = default;
}

This version will permits you to skip the body definition.

However, you have to follow some rules when you declare explicitly-defaulted-functions :

8.4.2 Explicitly-defaulted functions [dcl.fct.def.default]

A function definition of the form:

  attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt = default ;

is called an explicitly-defaulted definition. A function that is explicitly defaulted shall

  • be a special member function,

  • have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be "reference to non-const T", where T is the name of the member function’s class) as if it had been implicitly declared,

  • not have default arguments.

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

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