Title
Which special member functions can be defaulted?
Status
cd2
Section
9.5 [dcl.fct.def]
Submitter
Daveed Vandevoorde

Created on 2009-05-27.00:00:00 last changed 171 months ago

Messages

Date: 2010-03-15.00:00:00

[Voted into WP at March, 2010 meeting.]

Date: 2009-10-15.00:00:00

Proposed resolution (October, 2009):

  1. Change 9.5 [dcl.fct.def] paragraph 9 as follows:

  2. A function definition of the form:

      decl-specifier-seqopt attribute-specifieropt declarator = default ;

    is called an explicitly-defaulted definition. Only special member functions may be explicitly defaulted, and the implementation shall define them as if they had implicit definitions (11.4.5 [class.ctor], 11.4.7 [class.dtor], 11.4.5.3 [class.copy.ctor]). 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,

    • not have default arguments, and

    • not have an exception-specification.

    [Note: This implies that parameter types, return type, and cv-qualifiers must match the hypothetical implicit declaration. —end note] An explicitly-defaulted function may be declared constexpr only if it would have been implicitly declared as constexpr. If it is explicitly defaulted on its first declaration,

    • it shall be public,

    • it shall not be explicit,

    • it shall not be virtual,

    • it is implicitly considered to have the same exception-specification as if it had been implicitly declared (14.5 [except.spec]), and

    • in the case of a copy constructor or copy assignment operator, it shall have the same parameter type as if it had been implicitly declared.

    [Note: Such a special member function may be trivial, and thus its accessibility and explicitness should match the hypothetical implicit definition; see below. —end note] [Example:

      struct S {
        S(int a = 0) = default;              // ill-formed: default argument
        void operator=(const S&) = default;  // ill-formed: non-matching return type
        ~S() throw() = default;              // ill-formed: exception-specification
      private:
        S(S&);                               // OK: private copy constructor
      };
      S::S(S&) = default;                    // OK: defines copy constructor
    

    end example] Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (11.4.5 [class.ctor], 11.4.7 [class.dtor], 11.4.5.3 [class.copy.ctor]), which might mean defining them as deleted.A special member function that would be implicitly defined as deleted may be explicitly defaulted only on its first declaration, in which case it is defined as deleted. A special member function is user-provided if it is user-declared and not explicitly defaulted on its first declaration. A user-provided explicitly-defaulted function is defined at the point where it is explicitly defaulted. [Note:...

    [Editorial note: this change incorporates the overlapping portion of the resolution of issue 667.]

  3. Change 11.4.5 [class.ctor] paragraph 6 as follows:

  4. ...[Note: ...An explicitly-defaulted definition has no might have an implicit exception-specification, see 9.5 [dcl.fct.def]. —end note]

This resolution also resolves issue 905. See also issue 667.

Date: 2009-05-27.00:00:00

The only restriction placed on the use of “=default” in 9.5 [dcl.fct.def] paragraph 9 is that a defaulted function must be a special member function. However, there are many variations of declarations of special member functions, and it's not clear which of those should be able to be defaulted. Among the possibilities:

  • default arguments

  • by-value parameter for a copy assignment operator

  • exception specifications

  • arbitrary return values for copy assignment operators

  • a const reference parameter when the implicit function would have a non-const

Presumably, you should only be able to default a function if it is declared compatibly with the implicit declaration that would have been generated.

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: ready -> cd2
2009-11-08 00:00:00adminsetstatus: review -> ready
2009-09-29 00:00:00adminsetmessages: + msg2306
2009-08-03 00:00:00adminsetmessages: + msg2186
2009-08-03 00:00:00adminsetstatus: open -> review
2009-05-27 00:00:00admincreate