五规则和隐式删除函数

rule of five and implicitly deleted functions

据我了解,五规则是指导规则。虽然,我已经看到编译器在某些情况下可能会隐式删除函数。例如,当定义一个move-ctor'时,copy assignment/copy ctor'将被删除。

我想知道是否还有上面提到的场景。也就是说,自定义函数在哪些场景下可能会隐式删除其他函数?

谢谢

编辑:
对涵盖该主题的某些来源的引用也可以!

对于所有 "five",标准定义了它们将被隐式声明为已删除的情况。我已经为您命名并引用了 C++ 标准中的相关部分 N4659:

  • (12.3.3)定义联合时,可以隐式删除其中的5个:

    [..] [ 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) 当没有用户定义的替代项时,"five" 被隐式声明:

    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)构造函数的隐式删除:

    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) 隐式删除 copy/move 构造函数:

    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) 隐式删除copy/move赋值运算符:

    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) 析构函数的隐式删除:

    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.