自动生成默认/复制/移动ctor和复制/移动赋值运算符的条件? [英] Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

查看:236
本文介绍了自动生成默认/复制/移动ctor和复制/移动赋值运算符的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新我的记忆条件下,编译器通常会自动生成默认构造函数,复制构造函数和赋值运算符。

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator.

我似乎回忆有一些规则,但我不记得,也不能找到一个有信誉的资源在线。

I seem to recollect there were some rules, but I don't remeber, and also can't find a reputable resource online. Can anyone help?

推荐答案

在下文中,自动生成表示隐式声明为默认值, 。在某些情况下,声明了特殊成员函数,但定义为已删除。

In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted.


  • 默认构造函数是自动生成的

  • 如果没有用户声明的移动构造函数或移动赋值运算符(因为没有移动构造函数或移动),那么复制构造函数是自动生成的在C ++ 03中的赋值运算符,这简化为C ++ 03中的always(§12.8/ 8)。

  • 如果没有用户声明的移动构造函数或移动赋值操作符(§12.8/ 19)。

  • 如果没有用户声明的析构函数,析构函数是自动生成的li>
  • The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).
  • The copy constructor is auto-generated if there is no user-declared move constructor or move assignment operator (because there are no move constructors or move assignment operators in C++03, this simplifies to "always" in C++03) (§12.8/8).
  • The copy assignment operator is auto-generated if there is no user-declared move constructor or move assignment operator (§12.8/19).
  • The destructor is auto-generated if there is no user-declared destructor (§12.4/4).

  • 如果没有用户声明的复制构造函数,复制赋值操作符或析构函数,并且生成的移动构造函数是有效的(例如,如果不需要分配常量成员),则移动构造函数是自动生成的(§12.8/ 10 )。

  • 如果没有用户声明的复制构造函数,复制赋值操作符或析构函数,并且生成的移动赋值运算符有效,则自动生成移动赋值运算符如果不需要分配常数成员)(§12.8/ 21)。

这篇关于自动生成默认/复制/移动ctor和复制/移动赋值运算符的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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