简体   繁体   English

是否为派生类生成了移动构造函数/赋值运算符

[英]Are move constructors/assignment operators generated for derived classes

If class X is derived from class Y and class Y has any of: 如果X类是从Y类派生的,并且Y类具有以下任一项:

  • A user-declared copy constructor, 用户声明的副本构造函数,
  • A user-declared copy assignment operator, 用户声明的副本分配运算符,
  • A user-declared destructor 用户声明的析构函数
  • A user-declared move constructor, 用户声明的move构造函数,
  • A user-declared move assignment operator, 用户声明的移动分配运算符,

Will a move constructor and move assignment operator be implicitly defaulted for class X providing it declares none of the above? 如果X类没有声明上述任何一项,是否会将移动构造函数和移动赋值运算符隐式默认为类X?

eg 例如

struct Y
{
     virtual ~Y() {}

     // .... stuff

};

struct X : public Y
{
   // ... stuff but no destructor, 
   //               no copy/move assignment operator 
   //               no copy/move constructor

   // will X have a default move constructor / assignment operator?
};

I am currently using gcc, but I am mainly interested in what the correct behaviour should be (as opposed to whether or not a particular compiler is standards compliant).. 我目前正在使用gcc,但主要是对正确的行为(相对于特定的编译器是否符合标准)感兴趣。

As far as the derived class is concerned, the fact that an attribute or a base class has user-defined or implicit special functions does not matter. 就派生类而言,属性或基类具有用户定义的或隐式的特殊功能这一事实无关紧要。 All that matter is their presence. 重要的是他们的存在。

Compliant C++11 compilers should automatically provide the move constructors and assignment operators for the struct and class, if possible (which is clearly defined in the Standard), even though only classes with dynamically allocated buffers will really benefit from it (moving an int is just copying it). 兼容的C ++ 11编译器应自动为struct和类(在标准中已明确定义)提供struct和class的move构造函数和赋值运算符,即使只有具有动态分配缓冲区的类才能真正从中受益(移动int只是复制它)。

Therefore, if your class embeds a std::string or a std::unique_ptr (for example), then its move constructor will call the embedded string or unique_ptr move constructor and it will be efficient... for free. 因此,如果您的类嵌入了std::stringstd::unique_ptr (例如),则其move构造函数将调用嵌入的stringunique_ptr move构造函数,这将是有效的...免费的。

Simply changing the compilation mode should thus slightly improve performance. 只需更改编译模式即可稍微提高性能。

Yes, they are, in §12.8.9: 是的,在§12.8.9中:

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if 如果类X的定义未明确声明移动构造函数,则仅当且仅当将隐式声明为默认构造函数。

  • X does not have a user-declared copy constructor, X没有用户声明的副本构造函数,
  • X does not have a user-declared copy assignment operator, X没有用户声明的副本分配运算符,
  • X does not have a user-declared move assignment operator, X没有用户声明的移动分配运算符,
  • X does not have a user-declared destructor, and X没有用户声明的析构函数,并且
  • the move constructor would not be implicitly defined as deleted. move构造函数不会隐式定义为Delete。

and §12.8.10 和§12.8.10

The implicitly-declared move constructor for class X will have the form 类X隐式声明的move构造函数将具有以下形式

X::X(X&&)

Similarly, for move assignment operators in 12.8.20: 同样,对于12.8.20中的移动分配运算符:

If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if 如果类X的定义未明确声明移动赋值运算符,则仅当且仅当将隐式声明为默认值

  • X does not have a user-declared copy constructor, X没有用户声明的副本构造函数,
  • X does not have a user-declared move constructor, X没有用户声明的move构造函数,
  • X does not have a user-declared copy assignment operator, X没有用户声明的副本分配运算符,
  • X does not have a user-declared destructor, and X没有用户声明的析构函数,并且
  • the move assignment operator would not be implicitly defined as deleted. 移动分配运算符不会隐式定义为已删除。

Base classes don't come into it directly. 基类不直接进入它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除基类中的复制和移动构造函数/赋值运算符就足够了吗? - Is deleting copy and move constructors/assignment operators in base class enough? 删除默认C ++复制和移动构造函数和赋值运算符的缺点? - Disadvantages to deleting default C++ copy and move constructors and assignment operators? 如何检查默认的复制/移动构造函数/赋值运算符? - How can I inspect the default copy/move constructors/assignment operators? 复制构造函数和赋值运算符 - Copy constructors and Assignment Operators 如何为具有私有成员的派生类实现移动构造函数 - How to implement move-Constructors for derived classes with private members C++20:自动生成的运算符在派生类中不可引用? - C++20: Automatically generated operators are unreferencable in derived classes? 查找所有移动构造函数和移动赋值运算符(尤其是没有“ noexcept”的那些) - Find all move constructors and move assignment operators (particularly those without 'noexcept') 为什么移动构造函数和移动标准库的赋值运算符会使对象从未指定的状态移开? - Why do move constructors and move assignment operators of Standard Library leave the object moved-from in unspecified state? 移动构造函数和赋值运算符:为什么派生类没有默认值? - Move constructor and assignment operator: why no default for derived classes? 在将对象传递给函数的过程中删除了复制/移动构造函数和赋值运算符后,没有得到编译时错误 - Did not get a compile time error after deleting copy/move constructors and assignment operators during passing object to function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM