Compiled C ++ 11 Compiler Functions

Say class

class Piece {} ;

If I'm right, this 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 this?

a)

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

b)

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

with)

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

d)

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

e)

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

My compiler of my understanding will generate for:

  • a) Default constructor, Copy constructor, Copy assignment operator, (operator move / assign operator?)
  • b) Destructor
  • c) Destructor
  • d) Instance Assignment Operator and Destructor (move / assign statement operator?)
  • e) Default constructor, Copy constructor, Destructor, (move constructor / assignment operator?)

Am I correcting something or do not see something here?

Basically, does C++11 have any new rules for generating functions if they are not provided by the user?

+7
source share
2 answers

I will leave here some irrelevant points, for example. about union s, base classes, boolean or peer-initializers, etc. If your classes have members, base classes ... then the answer will be different. For example, if you have a const member, an implicitly declared assignment operator will be defined as remote.

default constructor

[class.ctor] / 5

The default constructor for class X is the constructor of class X , which can be called without an argument. If there is no user-declared constructor for class X , a constructor that has no parameters is implicitly declared as default. The implicitly declared default constructor is a member of the inline public its class. By default, the default constructor for class X is defined as remote if [... there are many non-local items].

Thus, in cases a) and e) [without any ctor declared by the user], ctor is declared by default by default.

default destructor

[class.dtor]

4 If the class does not have a user-declared destructor, the destructor is implicitly declared as default. An implicitly declared destructor is a member of the inline public its class.

5 The default destructor for class X is defined as remote if [... there are a lot of non-local items]

Thus, in all cases, but a) [with user declared dtor], dtor is by default declared and implicitly defined if odr is used.


According to [class.copy] / 2 + 3, copy-ctor and move-ctor may have additional parameters if they have default arguments.

copy constructor

Copy-ctor is declared implicitly if there is no custom copy-ctor (the ctor pattern is never a copy-ctor). [Class.copy] / 7

If the class definition declares a move constructor or moves an assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as default. 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 the user-declared copy-ctor], copy-ctor is declared implicitly. In cases b) and c) [with the user-provided ctor movement] copy-ctor is defined as deleted. For a) [user-declared dtor] and e) [user-declared copy-assignment op], it can be defined as default, but this is deprecated.

Constructor movement

Move-ctor will not even be declared in these cases [class.copy] / 9

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy destination operator,
  • X does not have a user-declared move destination operator,
  • X does not have a user-declared destructor and
  • the move constructor will not be implicitly defined as remote.

Once again, there are some cases when they will be defined as deleted, but they do not apply here.

Therefore, move-ctor is not declared in any of the cases.


copy assignment operator

In [class.copy] / 18:

If the class definition does not explicitly declare a copy assignment statement, one is declared implicitly. If a class definition declares a move constructor or moves an assignment operator, the implicitly declared copy assignment operator is defined as remote; otherwise, it is defined as default. The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.

In some cases, it is defined as remote, see [class.copy] / 23, but they do not apply here.

A copy-assign operation is declared in all cases, but e) [user-declared copy-assignment op]. It is defined as deleted in b) and c) [both: user-declared move ctor]; and it can be defined as default in a) [user-declared dtor] and d) [user-declared instance-ctor]. Note the parallel with copy-ctor.

call forwarding operator

Like move-ctor, the assignment operation move is not even declared if either [class.copy] / 20:

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared move constructor,
  • X does not have a user-declared copy destination operator,
  • X does not have a user-declared destructor and
  • the move assignment operator will not be implicitly defined as remote.

In some cases, it is defined as deleted, see [class.copy] / 23 (the same paragraph as copy-ctor), but they do not apply here.

The move-assignment-op is declared implicitly and is defaulted to default none .

+6
source

Therefore, looking at some publications and online tutorials, I came to the conclusion:

Generated Functions: -

  • C ++ 03:

    1) Default constructor (generated only if the constructor is not declared by the user)

    2) Copy the constructor (generated only if the number 5.6 declared by the user)

    3) Copy assignment operator (generated only if 5.6 is not declared by the user)

    4) Destructor

  • Since C ++ 11:

    5) Move the constructor (generated only if 2,3,4,6 is not declared by the user)

    6) Move assignment operator (generated only if 2,3,4,5 is not declared by the user)


So for

a)

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

b)

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

with)

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

d)

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

e)

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

Edit : according to DyP comment: -

In C ++ 11,

In case a), 2 and 3 are outdated.

In case d), 3 is deprecated.

In case e), 2 is deprecated.

+3
source

Source: https://habr.com/ru/post/956781/


All Articles