简体   繁体   English

C ++ 11编译器生成的函数

[英]C++11 compiler generated functions

Say a class 说一堂课

class Piece {} ;

If I'm correct that should be equivalent to : 如果我是正确的,那应该等同于:

class Piece {
   //C++ 03
    Piece ();                          //default constructor
    Piece( const Piece&);              //copy constructor
    Piece& operator=(const Piece&);    //copy assignment operator
    ~Piece();                          //destructor

    //Since C++ 11
    Piece(Piece&&);                   //move constructor
    Piece& operator=(Piece&&);        //move assignment operator
};

So what can I say about these ? 那么我能怎么说呢?

a) 一种)

class Pawn{
    ~Pawn() {}// Only destructor
};

b) b)

class Bishop{
    Bishop(Bishop&& ) {}// Only move constructor
};

c) C)

class Knight{
    Knight(Knight&&, int =0) {} // move constructor, when no second arg present
};

d) d)

class Rook {
    Rook(const Rook& ) {}// Only copy constructor
};

e) e)

class King{
        King& operator=(const King&) = delete;
    };

My per my understanding compiler will generate for : 我的理解编译器将为生成:

  • a) Default Constructor, Copy Constructor, Copy Assignment operator , ( move constructor/assignment operator ? ) a)默认构造函数,复制构造函数,复制分配运算符(移动构造函数/分配运算符?)
  • b) Destructor b)析构函数
  • c) Destructor c)析构函数
  • d) Copy Assignment operator and Destructor ( move constructor/assignment operator ? ) d)复制赋值运算符和析构函数(移动构造函数/赋值运算符?)
  • e) Default Constructor, Copy Constructor, Destructor, ( move constructor/assignment operator ? ) e)默认构造函数,复制构造函数,析构函数(移动构造函数/赋值运算符?)

Am I correct or missing something here ? 我在这里纠正或遗漏了什么吗?

Basically is C++11 has any new rules for generating functions, when not provided by user ? 基本上, C++11在用户未提供时会具有任何用于生成函数的新规则吗?

I'll leave out some irrelevant points here, eg about union s, base classes, brace-or-equal-initializers etc. If your classes have any members, base classes, ... then the answer will differ. 在这里,我将省略一些不相关的要点,例如,关于union ,基类, 大括号或相等初始化器等。如果您的类具有任何成员,基类,则答案将有所不同。 For example, if you have a const member, an implicitly declared assignment operator would be defined as deleted. 例如,如果您有一个const成员,则将一个隐式声明的赋值运算符定义为delete。

default constructor 默认构造函数

[class.ctor]/5 [class.ctor] / 5

A default constructor for a class X is a constructor of class X that can be called without an argument. 对于A类默认构造函数X是类的构造函数X ,可以不带参数调用。 If there is no user-declared constructor for class X , a constructor having no parameters is implicitly declared as defaulted. 如果类X没有用户声明的构造函数,则将不带参数的构造函数隐式声明为默认值。 An implicitly-declared default constructor is an inline public member of its class. 隐式声明的默认构造函数是其类的inline public成员。 A defaulted default constructor for class X is defined as deleted if [... lots of points irrelevant here]. 如果[...很多此处无关紧要的点],则将类X默认默认构造函数定义为已删除。

So in cases a) and e) [without any user-declared ctor], a default ctor is declared as defaulted. 因此,在情况a)和e)[没有任何用户声明的ctor]中,默认ctor被声明为默认值。

default destructor 默认析构函数

[class.dtor] [class.dtor]

4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted. 4如果类没有用户声明的析构函数,则将析构函数隐式声明为默认变量。 An implicitly-declared destructor is an inline public member of its class. 隐式声明的析构函数是其类的inline public成员。

5 A defaulted destructor for a class X is defined as deleted if [... lots of points irrelevant here] 5如果[...许多此处无关紧要的点],则将X类的默认析构函数定义为已删除。

So in all cases but a) [with a user-declared dtor], a default dtor is implicitly declared and implicitly defined if odr-used. 因此,除了a)[带有用户声明的dtor]之外,所有情况下,默认dtor都会被隐式声明和隐式定义(如果使用odr)。


As per [class.copy]/2+3, a copy-ctor and move-ctor may have additional parameters, if those have default arguments. 根据[class.copy] / 2 + 3,如果copy-ctor和move-ctor具有默认参数,则它们可能具有其他参数。

copy-constructor 复制构造函数

A copy-ctor is declared implicitly if there's no user-defined copy-ctor (a ctor template is never a copy-ctor). 如果没有用户定义的copy-ctor(ctor模板绝不是copy-ctor),则隐式声明copy-ctor。 [class.copy]/7 [class.copy] / 7

If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; 如果类定义声明了move构造函数或move赋值运算符,则隐式声明的copy构造函数将定义为delete; otherwise, it is defined as defaulted. 否则,将其定义为默认值。 The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor. 如果该类具有用户声明的副本分配运算符或用户声明的析构函数,则不建议使用后者。

That is, in all cases but d) [with a user-declared copy-ctor], a copy-ctor is declared implicitly. 也就是说,在除d)之外的所有情况下[使用用户声明的copy-ctor],都隐式声明了copy-ctor。 In cases b) and c) [with a user-provided move ctor], the copy-ctor is defined as deleted. 在b)和c)情况下(具有用户提供的移动ctor),将copy-ctor定义为已删除。 For a) [user-declared dtor] and e) [user-declared copy-assignment op], it may be defined as defaulted, but that's deprecated. 对于a)[用户声明的dtor]和e)[用户声明的副本分配op],可以将其定义为默认值,但已弃用。

move-constructor 移动构造器

The move-ctor won't even be declared in these cases [class.copy]/9 在这种情况下,甚至都不会声明move-ctor [class.copy] / 9

  • 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。

There are again quite some cases where it would be defined as deleted, but they don't apply here. 在很多情况下,它会被定义为已删除,但在这里并不适用。

Therefore, the move-ctor is not declared in any of the cases. 因此,在任何情况下都不会声明move-ctor。


copy-assignment operator 复制分配运算符

In [class.copy]/18: 在[class.copy] / 18中:

If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. 如果类定义没有显式声明一个副本分配运算符,则隐式声明一个。 If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; 如果类定义声明了move构造函数或move赋值运算符,则隐式声明的副本赋值运算符将定义为Delete; otherwise, it is defined as defaulted. 否则,将其定义为默认值。 The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor. 如果该类具有用户声明的副本构造函数或用户声明的析构函数,则不建议使用后一种情况。

It is defined as deleted in some cases, see [class.copy]/23, but they don't apply here. 在某些情况下,它被定义为已删除,请参阅[class.copy] / 23,但此处并不适用。

The copy-assignment op is declared in all cases but e) [user-declared copy-assignment op]. 在所有情况下都声明了副本分配op,但e)[用户声明的副本分配op]。 It is defined as deleted in b) and c) [both: user-declared move ctor]; 它在b)和c)中定义为已删除[both:user-declared move ctor]; and it may be defined as defaulted in a) [user-declared dtor] and d) [user-declared copy-ctor]. 并且可以在a)[用户声明的dtor]和d)[用户声明的copy-ctor]中将其定义为默认值。 Note the parallel to the copy-ctor. 请注意与copy-ctor平行。

move-assignment operator 移动分配运算符

Similar to the move-ctor, the move-assignment op is not even declared if either [class.copy]/20: 与move-ctor相似,如果[class.copy] / 20中的任何一个都没有声明move-assignment op:

  • 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. 移动分配运算符不会隐式定义为已删除。

It is defined as deleted in some cases, see [class.copy]/23 (same paragraph as for the copy-ctor), but they don't apply here. 在某些情况下,它被定义为已删除,请参阅[class.copy] / 23(与copy-ctor相同的段落),但此处不适用。

A move-assignment-op is declared implicitly and defined as defaulted in none of the cases. 任何情况下, 都未隐式声明move-assignment-op并将其定义为默认值。

So looking at some post and online tutorials, I concluded this : 因此,通过阅读一些后期教程和在线教程,我得出以下结论:

Generated functions :- 生成的函数:-

  • C++ 03: C ++ 03:

    1) Default Constructor (generated only if no constructor is declared by user) 1)默认构造函数(仅当用户未声明构造函数时才生成)

    2) Copy Constructor (generated only if No. 5,6 declared by user) 2)复制构造函数(仅当用户声明编号5,6时生成)

    3) Copy Assignment operator (generated only if 5,6 not declared by user) 3)复制分配运算符(仅当用户未声明5,6时生成)

    4) Destructor 4)析构函数

  • Since C++ 11: 从C ++ 11开始:

    5) Move Constructor (generated only if 2,3,4,6 not declared by user) 5)移动构造函数(仅当用户未声明2,3,4,6时生成)

    6) Move Assignment Operator (generated only if 2,3,4,5 not declared by user) 6)移动分配运算符(仅当用户未声明2,3,4,5时生成)


So, for 因此对于

a) 一种)

class Pawn{ //1, 2, 3
    ~Pawn() {}// Only destructor
};

b) b)

class Bishop{ //4
    Bishop(Bishop&& ) {}
};

c) C)

class Knight{ //4
    Knight(Knight&&, int =0) {} 
};

d) d)

class Rook { //3, 4
    Rook(const Rook& ) {}
};

e) e)

class King{ //1, 2, 4
        King& operator=(const King&) = delete;
    };

Edit : As per DyP comment :- 编辑 :根据DyP评论:

In C++11, 在C ++ 11中,

For case a), 2 and 3 are deprecated. 对于情况a),不推荐使用2和3。

For case d), 3 is deprecated. 对于情况d),不赞成使用3。

For case e), 2 is deprecated. 对于情况e),不推荐使用2。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM