`= default` move构造函数是否等效于成员级move构造函数? [英] Is a `=default` move constructor equivalent to a member-wise move constructor?

查看:125
本文介绍了`= default` move构造函数是否等效于成员级move构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这个

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; } 
}

与此等效

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;
}

?

推荐答案

是相同的.

但是

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.

但是,在声明explicitly-defaulted-functions时必须遵循一些规则:

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

8.4.2显式默认的功能[dcl.fct.def.default]

以下形式的函数定义:

  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,

具有相同的声明函数类型(除了可能不同的 ref-qualifiers ,并且在复制构造函数或复制赋值运算符的情况下,参数类型可以是对非常量T",其中T是成员函数的类的名称),就好像它已隐式声明一样,

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,

没有默认参数.

这篇关于`= default` move构造函数是否等效于成员级move构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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