tmp/tmppuposiym/{from.md → to.md}
RENAMED
|
@@ -21,22 +21,18 @@ Any informal reference to the body of a function should be interpreted
|
|
| 21 |
as a reference to the non-terminal *function-body*. The optional
|
| 22 |
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 23 |
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 24 |
only if it is a *member-declaration* ([[class.mem]]).
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
'D1 (' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 30 |
|
| 31 |
-
|
| 32 |
-
```
|
| 33 |
-
|
| 34 |
-
as described in [[dcl.fct]]. A function shall be defined only in
|
| 35 |
-
namespace or class scope.
|
| 36 |
-
|
| 37 |
-
a simple example of a complete function definition is
|
| 38 |
|
| 39 |
``` cpp
|
| 40 |
int max(int a, int b, int c) {
|
| 41 |
int m = (a > b) ? a : b;
|
| 42 |
return (m > c) ? m : c;
|
|
@@ -44,26 +40,30 @@ int max(int a, int b, int c) {
|
|
| 44 |
```
|
| 45 |
|
| 46 |
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 47 |
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 48 |
|
|
|
|
|
|
|
| 49 |
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 50 |
and [[class.init]].
|
| 51 |
|
| 52 |
-
A *cv-qualifier-seq*
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
[
|
| 56 |
|
| 57 |
Unused parameters need not be named. For example,
|
| 58 |
|
| 59 |
``` cpp
|
| 60 |
void print(int a, int) {
|
| 61 |
std::printf("a = %d\n",a);
|
| 62 |
}
|
| 63 |
```
|
| 64 |
|
|
|
|
|
|
|
| 65 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 66 |
block-scope object of static storage duration that is implicitly defined
|
| 67 |
(see [[basic.scope.block]]).
|
| 68 |
|
| 69 |
The function-local predefined variable `__func__` is defined as if a
|
|
@@ -73,20 +73,24 @@ definition of the form
|
|
| 73 |
static const char __func__[] = "function-name";
|
| 74 |
```
|
| 75 |
|
| 76 |
had been provided, where *function-name* is an *implementation-defined*
|
| 77 |
string. It is unspecified whether such a variable has an address
|
| 78 |
-
distinct from that of any other object in the program.[^
|
|
|
|
|
|
|
| 79 |
|
| 80 |
``` cpp
|
| 81 |
struct S {
|
| 82 |
S() : s(__func__) { } // OK
|
| 83 |
const char* s;
|
| 84 |
};
|
| 85 |
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 86 |
```
|
| 87 |
|
|
|
|
|
|
|
| 88 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 89 |
|
| 90 |
A function definition of the form:
|
| 91 |
|
| 92 |
``` bnf
|
|
@@ -102,52 +106,58 @@ explicitly defaulted shall
|
|
| 102 |
copy assignment operator, the parameter type may be “reference to
|
| 103 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 104 |
as if it had been implicitly declared, and
|
| 105 |
- not have default arguments.
|
| 106 |
|
| 107 |
-
An explicitly-defaulted function
|
| 108 |
-
would have been implicitly declared as
|
| 109 |
-
explicitly defaulted on its first
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
as if it had been implicitly declared ([[except.spec]]).
|
| 115 |
-
|
| 116 |
-
If a function that is explicitly defaulted has an explicit
|
| 117 |
-
*exception-specification* that is not compatible ([[except.spec]]) with
|
| 118 |
-
the *exception-specification* on the implicit declaration, then
|
| 119 |
|
| 120 |
- if the function is explicitly defaulted on its first declaration, it
|
| 121 |
is defined as deleted;
|
| 122 |
- otherwise, the program is ill-formed.
|
| 123 |
|
|
|
|
|
|
|
| 124 |
``` cpp
|
| 125 |
struct S {
|
| 126 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 127 |
S(int a = 0) = default; // ill-formed: default argument
|
| 128 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 129 |
-
~S()
|
| 130 |
private:
|
| 131 |
int i;
|
| 132 |
S(S&); // OK: private copy constructor
|
| 133 |
};
|
| 134 |
S::S(S&) = default; // OK: defines copy constructor
|
| 135 |
```
|
| 136 |
|
|
|
|
|
|
|
| 137 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 138 |
collectively called *defaulted* functions, and the implementation shall
|
| 139 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 140 |
[[class.copy]]), which might mean defining them as deleted. A function
|
| 141 |
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 142 |
or deleted on its first declaration. A user-provided
|
| 143 |
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 144 |
first declaration) is defined at the point where it is explicitly
|
| 145 |
defaulted; if such a function is implicitly defined as deleted, the
|
| 146 |
-
program is ill-formed.
|
|
|
|
|
|
|
| 147 |
declaration can provide efficient execution and concise definition while
|
| 148 |
-
enabling a stable binary interface to an evolving code
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
``` cpp
|
| 151 |
struct trivial {
|
| 152 |
trivial() = default;
|
| 153 |
trivial(const trivial&) = default;
|
|
@@ -161,10 +171,12 @@ struct nontrivial1 {
|
|
| 161 |
nontrivial1();
|
| 162 |
};
|
| 163 |
nontrivial1::nontrivial1() = default; // not first declaration
|
| 164 |
```
|
| 165 |
|
|
|
|
|
|
|
| 166 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 167 |
|
| 168 |
A function definition of the form:
|
| 169 |
|
| 170 |
``` bnf
|
|
@@ -173,29 +185,38 @@ attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-spe
|
|
| 173 |
|
| 174 |
is called a *deleted definition*. A function with a deleted definition
|
| 175 |
is also called a *deleted function*.
|
| 176 |
|
| 177 |
A program that refers to a deleted function implicitly or explicitly,
|
| 178 |
-
other than to declare it, is ill-formed.
|
| 179 |
-
function implicitly or explicitly and forming a pointer or
|
| 180 |
-
pointer-to-member to the function. It applies even for references in
|
| 181 |
-
expressions that are not potentially-evaluated. If a function is
|
| 182 |
-
overloaded, it is referenced only if the function is selected by
|
| 183 |
-
overload resolution.
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
initialization with
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
struct onlydouble {
|
| 190 |
onlydouble() = delete; // OK, but redundant
|
| 191 |
onlydouble(std::intmax_t) = delete;
|
| 192 |
onlydouble(double);
|
| 193 |
};
|
| 194 |
```
|
| 195 |
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
deleted definitions of a user-declared `operator new` for that class.
|
| 198 |
|
| 199 |
``` cpp
|
| 200 |
struct sometype {
|
| 201 |
void* operator new(std::size_t) = delete;
|
|
@@ -203,10 +224,14 @@ struct sometype {
|
|
| 203 |
};
|
| 204 |
sometype* p = new sometype; // error, deleted class operator new
|
| 205 |
sometype* q = new sometype[3]; // error, deleted class operator new[]
|
| 206 |
```
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
One can make a class uncopyable, i.e. move-only, by using deleted
|
| 209 |
definitions of the copy constructor and copy assignment operator, and
|
| 210 |
then providing defaulted definitions of the move constructor and move
|
| 211 |
assignment operator.
|
| 212 |
|
|
@@ -221,18 +246,29 @@ struct moveonly {
|
|
| 221 |
};
|
| 222 |
moveonly* p;
|
| 223 |
moveonly q(*p); // error, deleted copy constructor
|
| 224 |
```
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
``` cpp
|
| 233 |
struct sometype {
|
| 234 |
sometype();
|
| 235 |
};
|
| 236 |
sometype::sometype() = delete; // ill-formed; not first declaration
|
| 237 |
```
|
| 238 |
|
|
|
|
|
|
|
|
|
| 21 |
as a reference to the non-terminal *function-body*. The optional
|
| 22 |
*attribute-specifier-seq* in a *function-definition* appertains to the
|
| 23 |
function. A *virt-specifier-seq* can be part of a *function-definition*
|
| 24 |
only if it is a *member-declaration* ([[class.mem]]).
|
| 25 |
|
| 26 |
+
In a *function-definition*, either `void` *declarator* `;` or
|
| 27 |
+
*declarator* `;` shall be a well-formed function declaration as
|
| 28 |
+
described in [[dcl.fct]]. A function shall be defined only in namespace
|
| 29 |
+
or class scope.
|
| 30 |
|
| 31 |
+
[*Example 1*:
|
|
|
|
| 32 |
|
| 33 |
+
A simple example of a complete function definition is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
``` cpp
|
| 36 |
int max(int a, int b, int c) {
|
| 37 |
int m = (a > b) ? a : b;
|
| 38 |
return (m > c) ? m : c;
|
|
|
|
| 40 |
```
|
| 41 |
|
| 42 |
Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
|
| 43 |
`c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
|
| 44 |
|
| 45 |
+
— *end example*]
|
| 46 |
+
|
| 47 |
A *ctor-initializer* is used only in a constructor; see [[class.ctor]]
|
| 48 |
and [[class.init]].
|
| 49 |
|
| 50 |
+
[*Note 1*: A *cv-qualifier-seq* affects the type of `this` in the body
|
| 51 |
+
of a member function; see [[dcl.ref]]. — *end note*]
|
| 52 |
+
|
| 53 |
+
[*Note 2*:
|
| 54 |
|
| 55 |
Unused parameters need not be named. For example,
|
| 56 |
|
| 57 |
``` cpp
|
| 58 |
void print(int a, int) {
|
| 59 |
std::printf("a = %d\n",a);
|
| 60 |
}
|
| 61 |
```
|
| 62 |
|
| 63 |
+
— *end note*]
|
| 64 |
+
|
| 65 |
In the *function-body*, a *function-local predefined variable* denotes a
|
| 66 |
block-scope object of static storage duration that is implicitly defined
|
| 67 |
(see [[basic.scope.block]]).
|
| 68 |
|
| 69 |
The function-local predefined variable `__func__` is defined as if a
|
|
|
|
| 73 |
static const char __func__[] = "function-name";
|
| 74 |
```
|
| 75 |
|
| 76 |
had been provided, where *function-name* is an *implementation-defined*
|
| 77 |
string. It is unspecified whether such a variable has an address
|
| 78 |
+
distinct from that of any other object in the program.[^11]
|
| 79 |
+
|
| 80 |
+
[*Example 2*:
|
| 81 |
|
| 82 |
``` cpp
|
| 83 |
struct S {
|
| 84 |
S() : s(__func__) { } // OK
|
| 85 |
const char* s;
|
| 86 |
};
|
| 87 |
void f(const char* s = __func__); // error: __func__ is undeclared
|
| 88 |
```
|
| 89 |
|
| 90 |
+
— *end example*]
|
| 91 |
+
|
| 92 |
### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
|
| 93 |
|
| 94 |
A function definition of the form:
|
| 95 |
|
| 96 |
``` bnf
|
|
|
|
| 106 |
copy assignment operator, the parameter type may be “reference to
|
| 107 |
non-const `T`”, where `T` is the name of the member function’s class)
|
| 108 |
as if it had been implicitly declared, and
|
| 109 |
- not have default arguments.
|
| 110 |
|
| 111 |
+
An explicitly-defaulted function that is not defined as deleted may be
|
| 112 |
+
declared `constexpr` only if it would have been implicitly declared as
|
| 113 |
+
`constexpr`. If a function is explicitly defaulted on its first
|
| 114 |
+
declaration, it is implicitly considered to be `constexpr` if the
|
| 115 |
+
implicit declaration would be.
|
| 116 |
|
| 117 |
+
If a function that is explicitly defaulted is declared with a
|
| 118 |
+
*noexcept-specifier* that does not produce the same exception
|
| 119 |
+
specification as the implicit declaration ([[except.spec]]), then
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
- if the function is explicitly defaulted on its first declaration, it
|
| 122 |
is defined as deleted;
|
| 123 |
- otherwise, the program is ill-formed.
|
| 124 |
|
| 125 |
+
[*Example 1*:
|
| 126 |
+
|
| 127 |
``` cpp
|
| 128 |
struct S {
|
| 129 |
constexpr S() = default; // ill-formed: implicit S() is not constexpr
|
| 130 |
S(int a = 0) = default; // ill-formed: default argument
|
| 131 |
void operator=(const S&) = default; // ill-formed: non-matching return type
|
| 132 |
+
~S() noexcept(false) = default; // deleted: exception specification does not match
|
| 133 |
private:
|
| 134 |
int i;
|
| 135 |
S(S&); // OK: private copy constructor
|
| 136 |
};
|
| 137 |
S::S(S&) = default; // OK: defines copy constructor
|
| 138 |
```
|
| 139 |
|
| 140 |
+
— *end example*]
|
| 141 |
+
|
| 142 |
Explicitly-defaulted functions and implicitly-declared functions are
|
| 143 |
collectively called *defaulted* functions, and the implementation shall
|
| 144 |
provide implicit definitions for them ([[class.ctor]] [[class.dtor]],
|
| 145 |
[[class.copy]]), which might mean defining them as deleted. A function
|
| 146 |
is *user-provided* if it is user-declared and not explicitly defaulted
|
| 147 |
or deleted on its first declaration. A user-provided
|
| 148 |
explicitly-defaulted function (i.e., explicitly defaulted after its
|
| 149 |
first declaration) is defined at the point where it is explicitly
|
| 150 |
defaulted; if such a function is implicitly defined as deleted, the
|
| 151 |
+
program is ill-formed.
|
| 152 |
+
|
| 153 |
+
[*Note 1*: Declaring a function as defaulted after its first
|
| 154 |
declaration can provide efficient execution and concise definition while
|
| 155 |
+
enabling a stable binary interface to an evolving code
|
| 156 |
+
base. — *end note*]
|
| 157 |
+
|
| 158 |
+
[*Example 2*:
|
| 159 |
|
| 160 |
``` cpp
|
| 161 |
struct trivial {
|
| 162 |
trivial() = default;
|
| 163 |
trivial(const trivial&) = default;
|
|
|
|
| 171 |
nontrivial1();
|
| 172 |
};
|
| 173 |
nontrivial1::nontrivial1() = default; // not first declaration
|
| 174 |
```
|
| 175 |
|
| 176 |
+
— *end example*]
|
| 177 |
+
|
| 178 |
### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
|
| 179 |
|
| 180 |
A function definition of the form:
|
| 181 |
|
| 182 |
``` bnf
|
|
|
|
| 185 |
|
| 186 |
is called a *deleted definition*. A function with a deleted definition
|
| 187 |
is also called a *deleted function*.
|
| 188 |
|
| 189 |
A program that refers to a deleted function implicitly or explicitly,
|
| 190 |
+
other than to declare it, is ill-formed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
[*Note 1*: This includes calling the function implicitly or explicitly
|
| 193 |
+
and forming a pointer or pointer-to-member to the function. It applies
|
| 194 |
+
even for references in expressions that are not potentially-evaluated.
|
| 195 |
+
If a function is overloaded, it is referenced only if the function is
|
| 196 |
+
selected by overload resolution. The implicit odr-use (
|
| 197 |
+
[[basic.def.odr]]) of a virtual function does not, by itself, constitute
|
| 198 |
+
a reference. — *end note*]
|
| 199 |
+
|
| 200 |
+
[*Example 1*:
|
| 201 |
+
|
| 202 |
+
One can enforce non-default-initialization and non-integral
|
| 203 |
initialization with
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
struct onlydouble {
|
| 207 |
onlydouble() = delete; // OK, but redundant
|
| 208 |
onlydouble(std::intmax_t) = delete;
|
| 209 |
onlydouble(double);
|
| 210 |
};
|
| 211 |
```
|
| 212 |
|
| 213 |
+
— *end example*]
|
| 214 |
+
|
| 215 |
+
[*Example 2*:
|
| 216 |
+
|
| 217 |
+
One can prevent use of a class in certain *new-expression*s by using
|
| 218 |
deleted definitions of a user-declared `operator new` for that class.
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
struct sometype {
|
| 222 |
void* operator new(std::size_t) = delete;
|
|
|
|
| 224 |
};
|
| 225 |
sometype* p = new sometype; // error, deleted class operator new
|
| 226 |
sometype* q = new sometype[3]; // error, deleted class operator new[]
|
| 227 |
```
|
| 228 |
|
| 229 |
+
— *end example*]
|
| 230 |
+
|
| 231 |
+
[*Example 3*:
|
| 232 |
+
|
| 233 |
One can make a class uncopyable, i.e. move-only, by using deleted
|
| 234 |
definitions of the copy constructor and copy assignment operator, and
|
| 235 |
then providing defaulted definitions of the move constructor and move
|
| 236 |
assignment operator.
|
| 237 |
|
|
|
|
| 246 |
};
|
| 247 |
moveonly* p;
|
| 248 |
moveonly q(*p); // error, deleted copy constructor
|
| 249 |
```
|
| 250 |
|
| 251 |
+
— *end example*]
|
| 252 |
+
|
| 253 |
+
A deleted function is implicitly an inline function ([[dcl.inline]]).
|
| 254 |
+
|
| 255 |
+
[*Note 2*: The one-definition rule ([[basic.def.odr]]) applies to
|
| 256 |
+
deleted definitions. — *end note*]
|
| 257 |
+
|
| 258 |
+
A deleted definition of a function shall be the first declaration of the
|
| 259 |
+
function or, for an explicit specialization of a function template, the
|
| 260 |
+
first declaration of that specialization. An implicitly declared
|
| 261 |
+
allocation or deallocation function ([[basic.stc.dynamic]]) shall not
|
| 262 |
+
be defined as deleted.
|
| 263 |
+
|
| 264 |
+
[*Example 4*:
|
| 265 |
|
| 266 |
``` cpp
|
| 267 |
struct sometype {
|
| 268 |
sometype();
|
| 269 |
};
|
| 270 |
sometype::sometype() = delete; // ill-formed; not first declaration
|
| 271 |
```
|
| 272 |
|
| 273 |
+
— *end example*]
|
| 274 |
+
|