C++11 compiler generated functions

后端 未结 2 1150
终归单人心
终归单人心 2020-12-09 00:18

Say a class

class Piece {} ;

If I\'m correct that should be equivalent to :

class Piece {
   //C++ 03
    Piece ();                     


        
相关标签:
2条回答
  • 2020-12-09 00:37

    I'll leave out some irrelevant points here, e.g. about unions, base classes, brace-or-equal-initializers etc. If your classes have any members, base classes, ... then the answer will differ. For example, if you have a const member, an implicitly declared assignment operator would be defined as deleted.

    default constructor

    [class.ctor]/5

    A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class. A defaulted default constructor for class X is defined as deleted if [... lots of points irrelevant here].

    So in cases a) and e) [without any user-declared ctor], a default ctor is declared as defaulted.

    default destructor

    [class.dtor]

    4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted. An implicitly-declared destructor is an inline public member of its class.

    5 A defaulted destructor for a class X is defined as deleted if [... lots of points irrelevant here]

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


    As per [class.copy]/2+3, a copy-ctor and move-ctor may have additional parameters, if those have default arguments.

    copy-constructor

    A copy-ctor is declared implicitly if there's no user-defined copy-ctor (a ctor template is never a copy-ctor). [class.copy]/7

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

    move-constructor

    The move-ctor won't 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 assignment operator,
    • X does not have a user-declared move assignment operator,
    • X does not have a user-declared destructor, and
    • the move constructor would not be implicitly defined as deleted.

    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.


    copy-assignment operator

    In [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; 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.

    The copy-assignment op 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 may be defined as defaulted in a) [user-declared dtor] and d) [user-declared copy-ctor]. Note the parallel to the copy-ctor.

    move-assignment operator

    Similar to the move-ctor, the move-assignment op 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 assignment operator,
    • X does not have a user-declared destructor, and
    • 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.

    A move-assignment-op is declared implicitly and defined as defaulted in none of the cases.

    0 讨论(0)
  • 2020-12-09 00:45

    So looking at some post and online tutorials, I concluded this :

    Generated functions :-

    • C++ 03:

      1) Default Constructor (generated only if no constructor is declared by user)

      2) Copy Constructor (generated only if No. 5,6 declared by user)

      3) Copy Assignment operator (generated only if 5,6 not declared by user)

      4) Destructor

    • Since C++ 11:

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

      6) Move Assignment Operator (generated only if 2,3,4,5 not declared by user)


    So, for

    a)

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

    b)

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

    c)

    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 : As per DyP comment :-

    In C++11,

    For case a), 2 and 3 are deprecated.

    For case d), 3 is deprecated.

    For case e), 2 is deprecated.

    0 讨论(0)
提交回复
热议问题