Can non-special C++ member functions be deleted (or defaulted)?

前端 未结 1 2003
予麋鹿
予麋鹿 2020-12-19 00:56

A lot of attention has been received by = default and = delete with respect to the special members (default constructor, destructor, copy and move)

相关标签:
1条回答
  • 2020-12-19 01:37

    Any function whatsoever can be declared as deleted (using = delete). The standard does not impose any restrictions. This is covered by C++11[dcl.fct.def.delete]. It even gives an example of a deleted operator new.

    Defaulted functions, on the other hand, are limited by [dcl.fct.def.default]§1 as follows:

    1 ... A function that is explicitly defaulted shall

    • be a special member function,
    • have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function’s class) as if it had been implicitly declared, and
    • not have default arguments.

    The "special member functions" referenced in the first bullet point are (as per [special]§1):

    • default constructor
    • copy constructor
    • move constructor
    • copy assignment operator
    • move assignment operator
    • destructor
    0 讨论(0)
提交回复
热议问题