`=default` 移动构造函数是否等同于成员移动构造函数?

2023-09-27C/C++开发问题
2

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

问题描述

这是吗

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

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

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-specier-seqopt decl-specier-seqopt declarator virt-specier-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` 移动构造函数是否等同于成员移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6