Files
2025-10-25 03:02:53 +03:00

6.5 KiB
Raw Permalink Blame History

[dcl.fct.def.default]

9 Declarations [dcl]

9.6 Function definitions [dcl.fct.def]

9.6.2 Explicitly-defaulted functions [dcl.fct.def.default]

1

#

A function definition whosefunction-body is of the form= default ; is called an explicitly-defaulted definition.

A function that is explicitly defaulted shall

be a special member function ([special]) or a comparison operator function ([over.binary], [class.compare.default]), and

not have default arguments ([dcl.fct.default]).

2

#

An explicitly defaulted special member function F1 is allowed to differ from the corresponding special member function F2 that would have been implicitly declared, as follows:

F1 and F2 may have differing ref-qualifiers;

if F2 has an implicit object parameter of type “reference to C”, F1 may be an explicit object member function whose explicit object parameter is of (possibly different) type “reference to C”, in which case the type of F1 would differ from the type of F2 in that the type of F1 has an additional parameter;

F1 and F2 may have differing exception specifications; and

if F2 has a non-object parameter of type const C&, the corresponding non-object parameter of F1 may be of type C&.

If the type of F1 differs from the type of F2 in a way other than as allowed by the preceding rules, then:

if F1 is an assignment operator, and the return type of F1 differs from the return type of F2 or F1's non-object parameter type is not a reference, the program is ill-formed;

otherwise, if F1 is explicitly defaulted on its first declaration, it is defined as deleted;

otherwise, the program is ill-formed.

3

#

A function explicitly defaulted on its first declaration is implicitly inline ([dcl.inline]), and is implicitly constexpr ([dcl.constexpr]) if it is constexpr-suitable.

[Note 1:

Other defaulted functions are not implicitly constexpr.

— end note]

4

#

[Example 1: struct S { S(int a = 0) = default; // error: default argumentvoid operator=(const S&) = default; // error: non-matching return type~S() noexcept(false) = default; // OK, despite mismatched exception specificationprivate:int i; S(S&); // OK, private copy constructor}; S::S(S&) = default; // OK, defines copy constructorstruct T { T(); T(T &&) noexcept(false);};struct U { T t; U(); U(U &&) noexcept = default;}; U u1; U u2 = static_cast<U&&>(u1); // OK, calls std::terminate if T::T(T&&) throws — end example]

5

#

Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them ([class.ctor], [class.dtor], [class.copy.ctor], [class.copy.assign]) as described below, including possibly defining them as deleted.

A defaulted prospective destructor ([class.dtor]) that is not a destructor is defined as deleted.

A defaulted special member function that is neither a prospective destructor nor an eligible special member function ([special]) is defined as deleted.

A function isuser-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration.

A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is implicitly defined at the point where it is explicitly defaulted; if such a function is implicitly defined as deleted, the program is ill-formed.

[Note 2:

Declaring a function as defaulted after its first declaration can provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base.

— end note]

A non-user-provided defaulted function (i.e., implicitly declared or explicitly defaulted in the class) that is not defined as deleted is implicitly defined when it is odr-used ([basic.def.odr]) or needed for constant evaluation ([expr.const]).

[Note 3:

The implicit definition of a non-user-provided defaulted function does not bind any names.

— end note]

6

#

[Example 2: struct trivial { trivial() = default; trivial(const trivial&) = default; trivial(trivial&&) = default; trivial& operator=(const trivial&) = default; trivial& operator=(trivial&&) = default; ~trivial() = default;};

struct nontrivial1 { nontrivial1();}; nontrivial1::nontrivial1() = default; // not first declaration — end example]