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

标签 c++ c++11 constructor default move-semantics

这是吗

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/25848207/

相关文章:

c++ - 我必须做什么才能在数据区执行代码,(段保护)

c++ - C++ 中的 USB 串行验证

Java:将相同的对象传递给彼此的构造函数

c++ - 右值引用和构造函数参数

javascript - 是否可以将构造函数作为类中的方法?

c++ - Windows 上 C++ 的声音处理 - 朝着正确方向轻推

C++ If, else if 不读else if。或者 查找功能不正确?

c++ - 如何在编译时提取没有路径和后缀的源文件名?

c++ - 是什么导致了模板和继承中出现这种令人困惑的编译器错误?

c++ - 项目的 Makefile