rule of five and implicitly deleted functions [duplicate]
Asked Answered
F

1

6

For my understanding, the rule of five is a guidelince rule. Altough, I've seen that the compiler in some scenarios may delete functions, implicitly. For example, when defining a move-ctor', the copy assignment/ copy ctor' will be deleted.

I'd like to know if there are more scenario as the one mentioned. In other words, in which scenarios does user-defined function may delete implicitly other functions?

Thanks

EDIT:
a reference to some source which covers the subject would be fine too!

Fortunato answered 25/9, 2017 at 10:27 Comment(5)
There are only three special members that are always declared, implicitly so if not user-declared: copy constructor, copy assignment, and destructor. Each of their implied declarations may be a declaration-as-deleted if the operation isn't available.Sinistrous
And all of them are covered by the C++ standard, under "special member functions". Just look for the word "deleted" in that section.Porphyria
There was a nixe matrix about this somewhere (probably part of a talk at some conference), but for the love of god I can't find it right know.Accrue
@Accrue https://mcmap.net/q/382405/-what-are-the-rules-for-automatic-generation-of-move-operationsTrictrac
Note there's a difference in some cases between declaring a deleted function (which can be found by overload resolution but then fail) and not declaring the function at all (which will not be seen by overload resolution).Trictrac
J
4

For all of the "five", the standard defines the circumstances under which they will be implicitly declared as deleted. I have named and quoted the relevant sections for you from the C++ Standard N4659:

  • (12.3.3) When defining a union, some of the five can be implicitly deleted:

    [..] [ Note: Absent default member initializers (12.2), if any non-static data member of a union has a non-trivial default constructor (15.1), copy constructor (15.8), move constructor (15.8), copy assignment operator (15.8), move assignment operator (15.8), or destructor (15.4), the corresponding member function of the union must be user-provided or it will be implicitly deleted (11.4.3) for the union — end note ]

  • (15.1) The "five" are implicitly declared when there is no user defined alternative:

    The default constructor (15.1), copy constructor and copy assignment operator (15.8), move constructor and move assignment operator (15.8), and destructor (15.4) are special member functions . [ Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are odr-used (6.2). See 15.1, 15.4 and 15.8. — end note ]

  • (15.1.1) Implicit deletion of the constructor:

    A defaulted default constructor for class X is defined as deleted if:
    — (5.1) X is a union that has a variant member with a non-trivial default constructor and no variant member of X has a default member initializer,
    — (5.2) X is a non-union class that has a variant member M with a non-trivial default constructor and no variant member of the anonymous union containing M has a default member initializer,
    — (5.3) any non-static data member with no default member initializer (12.2) is of reference type,
    — (5.4) any non-variant non-static data member of const-qualified type (or array thereof) with no brace-or- equal-initializer does not have a user-provided default constructor,
    — (5.5) X is a union and all of its variant members are of const-qualified type (or array thereof),
    — (5.6) X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),
    — (5.7) any potentially constructed subobject, except for a non-static data member with a brace-or-equal- initializer , has class type M (or array thereof) and either M has no default constructor or overload resolution (16.3) as applied to find M ’s corresponding constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or
    — (5.8) any potentially constructed subobject has a type with a destructor that is deleted or inaccessible from the defaulted default constructor

  • (15.8.1.10) Implicit deletion of the copy/move constructor:

    A defaulted copy/move constructor for a class X is defined as deleted (11.4.3) if X has:
    — (10.1) a variant member with a non-trivial corresponding constructor and X is a union-like class,
    — (10.2) a potentially constructed subobject type M (or array thereof) that cannot be copied/moved because overload resolution (16.3), as applied to find M ’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
    — (10.3) any potentially constructed subobject of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or,
    — (10.4) for the copy constructor, a non-static data member of rvalue reference type. A defaulted move constructor that is defined as deleted is ignored by overload resolution (16.3, 16.4). [ Note: A deleted move constructor would otherwise interfere with initialization from an rvalue which can use the copy constructor instead. — end note ]

  • (15.8.2) Implicit deletion of copy/move assignment operator:

    A defaulted copy/move assignment operator for class X is defined as deleted if X has:
    — (7.1) a variant member with a non-trivial corresponding assignment operator and X is a union-like class, or
    — (7.2) a non-static data member of const non-class type (or array thereof), or
    — (7.3) a non-static data member of reference type, or
    — (7.4) a direct non-static data member of class type M (or array thereof) or a direct base class M that cannot be copied/moved because overload resolution (16.3), as applied to find M ’s corresponding assignment operator, results in an ambiguity or a function that is deleted or inaccessible from the defaulted assignment operator.

  • (15.4.5) Implicit deletion of the destructor:

    A defaulted destructor for a class X is defined as deleted if:
    — (5.1) X is a union-like class that has a variant member with a non-trivial destructor,
    — (5.2) any potentially constructed subobject has class type M (or array thereof) and M has a deleted destructor or a destructor that is inaccessible from the defaulted destructor,
    — (5.3) or, for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or in a function that is deleted or inaccessible from the defaulted destructor.

Jin answered 25/9, 2017 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.