tmp/tmpn6rrxh4i/{from.md → to.md}
RENAMED
|
@@ -1,65 +1,81 @@
|
|
| 1 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 2 |
|
| 3 |
-
A function definition of the form
|
| 4 |
-
|
| 5 |
-
``` bnf
|
| 6 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = default ;'
|
| 7 |
-
```
|
| 8 |
-
|
| 9 |
is called an *explicitly-defaulted* definition. A function that is
|
| 10 |
explicitly defaulted shall
|
| 11 |
|
| 12 |
-
- be a special member function
|
| 13 |
-
|
| 14 |
-
*ref-qualifier*s and except that in the case of a copy constructor or
|
| 15 |
-
copy assignment operator, the parameter type may be “reference to
|
| 16 |
-
non-const `T`”, where `T` is the name of the member function’s class)
|
| 17 |
-
as if it had been implicitly declared, and
|
| 18 |
- not have default arguments.
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
declaration, it is implicitly considered to be `constexpr` if the
|
| 24 |
-
implicit declaration would be.
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
is defined as deleted;
|
| 32 |
- otherwise, the program is ill-formed.
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
[*Example 1*:
|
| 35 |
|
| 36 |
``` cpp
|
| 37 |
struct S {
|
| 38 |
-
constexpr S() = default; //
|
| 39 |
-
S(int a = 0) = default; //
|
| 40 |
-
void operator=(const S&) = default; //
|
| 41 |
-
~S() noexcept(false) = default; //
|
| 42 |
private:
|
| 43 |
int i;
|
| 44 |
S(S&); // OK: private copy constructor
|
| 45 |
};
|
| 46 |
S::S(S&) = default; // OK: defines copy constructor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
```
|
| 48 |
|
| 49 |
— *end example*]
|
| 50 |
|
| 51 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 52 |
collectively called *defaulted* functions, and the implementation shall
|
| 53 |
-
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 54 |
-
[[class.copy]]), which might mean defining
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
[*Note 1*: Declaring a function as defaulted after its first
|
| 63 |
declaration can provide efficient execution and concise definition while
|
| 64 |
enabling a stable binary interface to an evolving code
|
| 65 |
base. — *end note*]
|
|
|
|
| 1 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 2 |
|
| 3 |
+
A function definition whose *function-body* is of the form `= default ;`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
is called an *explicitly-defaulted* definition. A function that is
|
| 5 |
explicitly defaulted shall
|
| 6 |
|
| 7 |
+
- be a special member function or a comparison operator function
|
| 8 |
+
[[over.binary]], and
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
- not have default arguments.
|
| 10 |
|
| 11 |
+
The type `T`₁ of an explicitly defaulted special member function `F` is
|
| 12 |
+
allowed to differ from the type `T`₂ it would have had if it were
|
| 13 |
+
implicitly declared, as follows:
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
- `T`₁ and `T`₂ may have differing *ref-qualifier*s;
|
| 16 |
+
- `T`₁ and `T`₂ may have differing exception specifications; and
|
| 17 |
+
- if `T`₂ has a parameter of type `const C&`, the corresponding
|
| 18 |
+
parameter of `T`₁ may be of type `C&`.
|
| 19 |
|
| 20 |
+
If `T`₁ differs from `T`₂ in any other way, then:
|
| 21 |
+
|
| 22 |
+
- if `F` is an assignment operator, and the return type of `T`₁ differs
|
| 23 |
+
from the return type of `T`₂ or `T`₁’s parameter type is not a
|
| 24 |
+
reference, the program is ill-formed;
|
| 25 |
+
- otherwise, if `F` is explicitly defaulted on its first declaration, it
|
| 26 |
is defined as deleted;
|
| 27 |
- otherwise, the program is ill-formed.
|
| 28 |
|
| 29 |
+
An explicitly-defaulted function that is not defined as deleted may be
|
| 30 |
+
declared `constexpr` or `consteval` only if it is constexpr-compatible (
|
| 31 |
+
[[special]], [[class.compare.default]]). A function explicitly defaulted
|
| 32 |
+
on its first declaration is implicitly inline [[dcl.inline]], and is
|
| 33 |
+
implicitly constexpr [[dcl.constexpr]] if it is constexpr-compatible.
|
| 34 |
+
|
| 35 |
[*Example 1*:
|
| 36 |
|
| 37 |
``` cpp
|
| 38 |
struct S {
|
| 39 |
+
constexpr S() = default; // error: implicit S() is not constexpr
|
| 40 |
+
S(int a = 0) = default; // error: default argument
|
| 41 |
+
void operator=(const S&) = default; // error: non-matching return type
|
| 42 |
+
~S() noexcept(false) = default; // OK, despite mismatched exception specification
|
| 43 |
private:
|
| 44 |
int i;
|
| 45 |
S(S&); // OK: private copy constructor
|
| 46 |
};
|
| 47 |
S::S(S&) = default; // OK: defines copy constructor
|
| 48 |
+
|
| 49 |
+
struct T {
|
| 50 |
+
T();
|
| 51 |
+
T(T &&) noexcept(false);
|
| 52 |
+
};
|
| 53 |
+
struct U {
|
| 54 |
+
T t;
|
| 55 |
+
U();
|
| 56 |
+
U(U &&) noexcept = default;
|
| 57 |
+
};
|
| 58 |
+
U u1;
|
| 59 |
+
U u2 = static_cast<U&&>(u1); // OK, calls std::terminate if T::T(T&&) throws
|
| 60 |
```
|
| 61 |
|
| 62 |
— *end example*]
|
| 63 |
|
| 64 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 65 |
collectively called *defaulted* functions, and the implementation shall
|
| 66 |
+
provide implicit definitions for them ([[class.ctor]], [[class.dtor]],
|
| 67 |
+
[[class.copy.ctor]], [[class.copy.assign]]), which might mean defining
|
| 68 |
+
them as deleted. A defaulted prospective destructor [[class.dtor]] that
|
| 69 |
+
is not a destructor is defined as deleted. A defaulted special member
|
| 70 |
+
function that is neither a prospective destructor nor an eligible
|
| 71 |
+
special member function [[special]] is defined as deleted. A function is
|
| 72 |
+
*user-provided* if it is user-declared and not explicitly defaulted or
|
| 73 |
+
deleted on its first declaration. A user-provided explicitly-defaulted
|
| 74 |
+
function (i.e., explicitly defaulted after its first declaration) is
|
| 75 |
+
defined at the point where it is explicitly defaulted; if such a
|
| 76 |
+
function is implicitly defined as deleted, the program is ill-formed.
|
| 77 |
|
| 78 |
[*Note 1*: Declaring a function as defaulted after its first
|
| 79 |
declaration can provide efficient execution and concise definition while
|
| 80 |
enabling a stable binary interface to an evolving code
|
| 81 |
base. — *end note*]
|