gpt4 book ai didi

c++ - `=default` move 构造函数是否等同于成员 move 构造函数?

转载 作者:IT老高 更新时间:2023-10-28 12:00:23 24 4
gpt4 key购买 nike

这是

struct Example { 
string a, b;

Example(Example&& mE) : a{move(mE.a)}, b{move(mE.b)} { }
Example& operator=(Example&& mE) { a = move(mE.a); b = move(mE.b); return *this; }
}

相当于这个

struct Example { 
string a, b;

Example(Example&& mE) = default;
Example& operator=(Example&& mE) = default;
}

?

最佳答案

是的,两者都是一样的。

但是

struct Example { 
string a, b;

Example(Example&& mE) = default;
Example& operator=(Example&& mE) = default;
}

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

但是,当你声明 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.

关于c++ - `=default` move 构造函数是否等同于成员 move 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com