From Jason Turner

[dcl.fct.def]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp5s_1od26/{from.md → to.md} +307 -61
tmp/tmp5s_1od26/{from.md → to.md} RENAMED
@@ -5,30 +5,34 @@
5
  Function definitions have the form
6
 
7
  ``` bnf
8
  function-definition:
9
  attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ function-body
 
10
  ```
11
 
12
  ``` bnf
13
  function-body:
14
  ctor-initializerₒₚₜ compound-statement
15
  function-try-block
16
- '= default ;'
17
- '= delete ;'
18
  ```
19
 
20
  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
  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
 
@@ -71,13 +75,13 @@ definition of the form
71
 
72
  ``` cpp
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 {
@@ -89,68 +93,84 @@ void f(const char* s = __func__); // error: __func__ is undeclared
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
97
- attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = default ;'
98
- ```
99
-
100
  is called an *explicitly-defaulted* definition. A function that is
101
  explicitly defaulted shall
102
 
103
- - be a special member function,
104
- - have the same declared function type (except for possibly differing
105
- *ref-qualifier*s and except that in the case of a copy constructor or
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*]
@@ -175,39 +195,35 @@ nontrivial1::nontrivial1() = default; // not first declaration
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
183
- attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ ' = delete ;'
184
- ```
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*]
@@ -220,19 +236,19 @@ deleted definitions of a user-declared `operator new` for that class.
220
  ``` cpp
221
  struct sometype {
222
  void* operator new(std::size_t) = delete;
223
  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
 
238
  ``` cpp
@@ -243,32 +259,262 @@ struct moveonly {
243
  moveonly& operator=(const moveonly&) = delete;
244
  moveonly& operator=(moveonly&&) = default;
245
  ~moveonly() = default;
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  Function definitions have the form
6
 
7
  ``` bnf
8
  function-definition:
9
  attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ function-body
10
+ attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator requires-clause function-body
11
  ```
12
 
13
  ``` bnf
14
  function-body:
15
  ctor-initializerₒₚₜ compound-statement
16
  function-try-block
17
+ '=' default ';'
18
+ '=' delete ';'
19
  ```
20
 
21
  Any informal reference to the body of a function should be interpreted
22
  as a reference to the non-terminal *function-body*. The optional
23
  *attribute-specifier-seq* in a *function-definition* appertains to the
24
  function. A *virt-specifier-seq* can be part of a *function-definition*
25
+ only if it is a *member-declaration* [[class.mem]].
26
 
27
  In a *function-definition*, either `void` *declarator* `;` or
28
  *declarator* `;` shall be a well-formed function declaration as
29
  described in  [[dcl.fct]]. A function shall be defined only in namespace
30
+ or class scope. The type of a parameter or the return type for a
31
+ function definition shall not be a (possibly cv-qualified) class type
32
+ that is incomplete or abstract within the function body unless the
33
+ function is deleted [[dcl.fct.def.delete]].
34
 
35
  [*Example 1*:
36
 
37
  A simple example of a complete function definition is
38
 
 
75
 
76
  ``` cpp
77
  static const char __func__[] = "function-name";
78
  ```
79
 
80
+ had been provided, where `function-name` is an *implementation-defined*
81
  string. It is unspecified whether such a variable has an address
82
+ distinct from that of any other object in the program.[^8]
83
 
84
  [*Example 2*:
85
 
86
  ``` cpp
87
  struct S {
 
93
 
94
  — *end example*]
95
 
96
  ### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
97
 
98
+ A function definition whose *function-body* is of the form `= default ;`
 
 
 
 
 
99
  is called an *explicitly-defaulted* definition. A function that is
100
  explicitly defaulted shall
101
 
102
+ - be a special member function or a comparison operator function
103
+ [[over.binary]], and
 
 
 
 
104
  - not have default arguments.
105
 
106
+ The type `T`₁ of an explicitly defaulted special member function `F` is
107
+ allowed to differ from the type `T`₂ it would have had if it were
108
+ implicitly declared, as follows:
 
 
109
 
110
+ - `T`₁ and `T`₂ may have differing *ref-qualifier*s;
111
+ - `T`₁ and `T`₂ may have differing exception specifications; and
112
+ - if `T`₂ has a parameter of type `const C&`, the corresponding
113
+ parameter of `T`₁ may be of type `C&`.
114
 
115
+ If `T`₁ differs from `T`₂ in any other way, then:
116
+
117
+ - if `F` is an assignment operator, and the return type of `T`₁ differs
118
+ from the return type of `T`₂ or `T`₁’s parameter type is not a
119
+ reference, the program is ill-formed;
120
+ - otherwise, if `F` is explicitly defaulted on its first declaration, it
121
  is defined as deleted;
122
  - otherwise, the program is ill-formed.
123
 
124
+ An explicitly-defaulted function that is not defined as deleted may be
125
+ declared `constexpr` or `consteval` only if it is constexpr-compatible (
126
+ [[special]], [[class.compare.default]]). A function explicitly defaulted
127
+ on its first declaration is implicitly inline [[dcl.inline]], and is
128
+ implicitly constexpr [[dcl.constexpr]] if it is constexpr-compatible.
129
+
130
  [*Example 1*:
131
 
132
  ``` cpp
133
  struct S {
134
+ constexpr S() = default; // error: implicit S() is not constexpr
135
+ S(int a = 0) = default; // error: default argument
136
+ void operator=(const S&) = default; // error: non-matching return type
137
+ ~S() noexcept(false) = default; // OK, despite mismatched exception specification
138
  private:
139
  int i;
140
  S(S&); // OK: private copy constructor
141
  };
142
  S::S(S&) = default; // OK: defines copy constructor
143
+
144
+ struct T {
145
+ T();
146
+ T(T &&) noexcept(false);
147
+ };
148
+ struct U {
149
+ T t;
150
+ U();
151
+ U(U &&) noexcept = default;
152
+ };
153
+ U u1;
154
+ U u2 = static_cast<U&&>(u1); // OK, calls std::terminate if T::T(T&&) throws
155
  ```
156
 
157
  — *end example*]
158
 
159
  Explicitly-defaulted functions and implicitly-declared functions are
160
  collectively called *defaulted* functions, and the implementation shall
161
+ provide implicit definitions for them ([[class.ctor]], [[class.dtor]],
162
+ [[class.copy.ctor]], [[class.copy.assign]]), which might mean defining
163
+ them as deleted. A defaulted prospective destructor [[class.dtor]] that
164
+ is not a destructor is defined as deleted. A defaulted special member
165
+ function that is neither a prospective destructor nor an eligible
166
+ special member function [[special]] is defined as deleted. A function is
167
+ *user-provided* if it is user-declared and not explicitly defaulted or
168
+ deleted on its first declaration. A user-provided explicitly-defaulted
169
+ function (i.e., explicitly defaulted after its first declaration) is
170
+ defined at the point where it is explicitly defaulted; if such a
171
+ function is implicitly defined as deleted, the program is ill-formed.
172
 
173
  [*Note 1*: Declaring a function as defaulted after its first
174
  declaration can provide efficient execution and concise definition while
175
  enabling a stable binary interface to an evolving code
176
  base. — *end note*]
 
195
 
196
  — *end example*]
197
 
198
  ### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
199
 
200
+ A function definition whose *function-body* is of the form `= delete ;`
 
 
 
 
 
201
  is called a *deleted definition*. A function with a deleted definition
202
  is also called a *deleted function*.
203
 
204
  A program that refers to a deleted function implicitly or explicitly,
205
  other than to declare it, is ill-formed.
206
 
207
  [*Note 1*: This includes calling the function implicitly or explicitly
208
  and forming a pointer or pointer-to-member to the function. It applies
209
  even for references in expressions that are not potentially-evaluated.
210
  If a function is overloaded, it is referenced only if the function is
211
+ selected by overload resolution. The implicit odr-use [[basic.def.odr]]
212
+ of a virtual function does not, by itself, constitute a
213
+ reference. — *end note*]
214
 
215
  [*Example 1*:
216
 
217
+ One can prevent default initialization and initialization by
218
+ non-`double`s with
219
 
220
  ``` cpp
221
  struct onlydouble {
222
  onlydouble() = delete; // OK, but redundant
223
+ template<class T>
224
+ onlydouble(T) = delete;
225
  onlydouble(double);
226
  };
227
  ```
228
 
229
  — *end example*]
 
236
  ``` cpp
237
  struct sometype {
238
  void* operator new(std::size_t) = delete;
239
  void* operator new[](std::size_t) = delete;
240
  };
241
+ sometype* p = new sometype; // error: deleted class operator new
242
+ sometype* q = new sometype[3]; // error: deleted class operator new[]
243
  ```
244
 
245
  — *end example*]
246
 
247
  [*Example 3*:
248
 
249
+ One can make a class uncopyable, i.e., move-only, by using deleted
250
  definitions of the copy constructor and copy assignment operator, and
251
  then providing defaulted definitions of the move constructor and move
252
  assignment operator.
253
 
254
  ``` cpp
 
259
  moveonly& operator=(const moveonly&) = delete;
260
  moveonly& operator=(moveonly&&) = default;
261
  ~moveonly() = default;
262
  };
263
  moveonly* p;
264
+ moveonly q(*p); // error: deleted copy constructor
265
  ```
266
 
267
  — *end example*]
268
 
269
+ A deleted function is implicitly an inline function [[dcl.inline]].
270
 
271
+ [*Note 2*: The one-definition rule [[basic.def.odr]] applies to deleted
272
+ definitions. — *end note*]
273
 
274
  A deleted definition of a function shall be the first declaration of the
275
  function or, for an explicit specialization of a function template, the
276
  first declaration of that specialization. An implicitly declared
277
+ allocation or deallocation function [[basic.stc.dynamic]] shall not be
278
+ defined as deleted.
279
 
280
  [*Example 4*:
281
 
282
  ``` cpp
283
  struct sometype {
284
  sometype();
285
  };
286
+ sometype::sometype() = delete; // error: not first declaration
287
  ```
288
 
289
  — *end example*]
290
 
291
+ ### Coroutine definitions <a id="dcl.fct.def.coroutine">[[dcl.fct.def.coroutine]]</a>
292
+
293
+ A function is a *coroutine* if its *function-body* encloses a
294
+ *coroutine-return-statement* [[stmt.return.coroutine]], an
295
+ *await-expression* [[expr.await]], or a *yield-expression*
296
+ [[expr.yield]]. The *parameter-declaration-clause* of the coroutine
297
+ shall not terminate with an ellipsis that is not part of a
298
+ *parameter-declaration*.
299
+
300
+ [*Example 1*:
301
+
302
+ ``` cpp
303
+ task<int> f();
304
+
305
+ task<void> g1() {
306
+ int i = co_await f();
307
+ std::cout << "f() => " << i << std::endl;
308
+ }
309
+
310
+ template <typename... Args>
311
+ task<void> g2(Args&&...) { // OK, ellipsis is a pack expansion
312
+ int i = co_await f();
313
+ std::cout << "f() => " << i << std::endl;
314
+ }
315
+
316
+ task<void> g3(int a, ...) { // error: variable parameter list not allowed
317
+ int i = co_await f();
318
+ std::cout << "f() => " << i << std::endl;
319
+ }
320
+ ```
321
+
322
+ — *end example*]
323
+
324
+ The *promise type* of a coroutine is
325
+ `std::coroutine_traits<R, P₁, …, Pₙ>::promise_type`, where `R` is the
326
+ return type of the function, and `P₁` … `Pₙ` are the sequence of types
327
+ of the function parameters, preceded by the type of the implicit object
328
+ parameter [[over.match.funcs]] if the coroutine is a non-static member
329
+ function. The promise type shall be a class type.
330
+
331
+ In the following, `pᵢ` is an lvalue of type `Pᵢ`, where `p₁` denotes
332
+ `*this` and `p_i+1` denotes the $i^\textrm{th}$ function parameter for a
333
+ non-static member function, and `pᵢ` denotes the $i^\textrm{th}$
334
+ function parameter otherwise.
335
+
336
+ A coroutine behaves as if its *function-body* were replaced by:
337
+
338
+ ``` bnf
339
+ '{'
340
+ *promise-type* promise *promise-constructor-arguments* ';'
341
+ % FIXME: promise'.get_return_object()' ';'
342
+ % ... except that it's not a discarded-value expression
343
+ 'try' '{'
344
+ 'co_await' 'promise.initial_suspend()' ';'
345
+ function-body
346
+ '} catch ( ... ) {'
347
+ 'if (!initial-await-resume-called)'
348
+ 'throw' ';'
349
+ 'promise.unhandled_exception()' ';'
350
+ '}'
351
+ final-suspend ':'
352
+ 'co_await' 'promise.final_suspend()' ';'
353
+ '}'
354
+ ```
355
+
356
+ where
357
+
358
+ - the *await-expression* containing the call to `initial_suspend` is the
359
+ *initial suspend point*, and
360
+ - the *await-expression* containing the call to `final_suspend` is the
361
+ *final suspend point*, and
362
+ - *initial-await-resume-called* is initially `false` and is set to
363
+ `true` immediately before the evaluation of the *await-resume*
364
+ expression [[expr.await]] of the initial suspend point, and
365
+ - *promise-type* denotes the promise type, and
366
+ - the object denoted by the exposition-only name *`promise`* is the
367
+ *promise object* of the coroutine, and
368
+ - the label denoted by the name *`final-suspend`* is defined for
369
+ exposition only [[stmt.return.coroutine]], and
370
+ - *promise-constructor-arguments* is determined as follows: overload
371
+ resolution is performed on a promise constructor call created by
372
+ assembling an argument list with lvalues `p₁` … `pₙ`. If a viable
373
+ constructor is found [[over.match.viable]], then
374
+ *promise-constructor-arguments* is `(p₁, …, pₙ)`, otherwise
375
+ *promise-constructor-arguments* is empty.
376
+
377
+ The *unqualified-id*s `return_void` and `return_value` are looked up in
378
+ the scope of the promise type. If both are found, the program is
379
+ ill-formed.
380
+
381
+ [*Note 1*: If the *unqualified-id* `return_void` is found, flowing off
382
+ the end of a coroutine is equivalent to a `co_return` with no operand.
383
+ Otherwise, flowing off the end of a coroutine results in undefined
384
+ behavior [[stmt.return.coroutine]]. — *end note*]
385
+
386
+ The expression `promise.get_return_object()` is used to initialize the
387
+ glvalue result or prvalue result object of a call to a coroutine. The
388
+ call to `get_return_object` is sequenced before the call to
389
+ `initial_suspend` and is invoked at most once.
390
+
391
+ A suspended coroutine can be resumed to continue execution by invoking a
392
+ resumption member function [[coroutine.handle.resumption]] of a
393
+ coroutine handle [[coroutine.handle]] that refers to the coroutine. The
394
+ function that invoked a resumption member function is called the
395
+ *resumer*. Invoking a resumption member function for a coroutine that is
396
+ not suspended results in undefined behavior.
397
+
398
+ An implementation may need to allocate additional storage for a
399
+ coroutine. This storage is known as the *coroutine state* and is
400
+ obtained by calling a non-array allocation function
401
+ [[basic.stc.dynamic.allocation]]. The allocation function’s name is
402
+ looked up in the scope of the promise type. If this lookup fails, the
403
+ allocation function’s name is looked up in the global scope. If the
404
+ lookup finds an allocation function in the scope of the promise type,
405
+ overload resolution is performed on a function call created by
406
+ assembling an argument list. The first argument is the amount of space
407
+ requested, and has type `std::size_t`. The lvalues `p₁` … `pₙ` are the
408
+ succeeding arguments. If no viable function is found
409
+ [[over.match.viable]], overload resolution is performed again on a
410
+ function call created by passing just the amount of space required as an
411
+ argument of type `std::size_t`.
412
+
413
+ The *unqualified-id* `get_return_object_on_allocation_failure` is looked
414
+ up in the scope of the promise type by class member access lookup
415
+ [[basic.lookup.classref]]. If any declarations are found, then the
416
+ result of a call to an allocation function used to obtain storage for
417
+ the coroutine state is assumed to return `nullptr` if it fails to obtain
418
+ storage, and if a global allocation function is selected, the
419
+ `::operator new(size_t, nothrow_t)` form is used. The allocation
420
+ function used in this case shall have a non-throwing
421
+ *noexcept-specification*. If the allocation function returns `nullptr`,
422
+ the coroutine returns control to the caller of the coroutine and the
423
+ return value is obtained by a call to
424
+ `T::get_return_object_on_allocation_failure()`, where `T` is the promise
425
+ type.
426
+
427
+ [*Example 2*:
428
+
429
+ ``` cpp
430
+ #include <iostream>
431
+ #include <coroutine>
432
+
433
+ // ::operator new(size_t, nothrow_t) will be used if allocation is needed
434
+ struct generator {
435
+ struct promise_type;
436
+ using handle = std::coroutine_handle<promise_type>;
437
+ struct promise_type {
438
+ int current_value;
439
+ static auto get_return_object_on_allocation_failure() { return generator{nullptr}; }
440
+ auto get_return_object() { return generator{handle::from_promise(*this)}; }
441
+ auto initial_suspend() { return std::suspend_always{}; }
442
+ auto final_suspend() { return std::suspend_always{}; }
443
+ void unhandled_exception() { std::terminate(); }
444
+ void return_void() {}
445
+ auto yield_value(int value) {
446
+ current_value = value;
447
+ return std::suspend_always{};
448
+ }
449
+ };
450
+ bool move_next() { return coro ? (coro.resume(), !coro.done()) : false; }
451
+ int current_value() { return coro.promise().current_value; }
452
+ generator(generator const&) = delete;
453
+ generator(generator && rhs) : coro(rhs.coro) { rhs.coro = nullptr; }
454
+ ~generator() { if (coro) coro.destroy(); }
455
+ private:
456
+ generator(handle h) : coro(h) {}
457
+ handle coro;
458
+ };
459
+ generator f() { co_yield 1; co_yield 2; }
460
+ int main() {
461
+ auto g = f();
462
+ while (g.move_next()) std::cout << g.current_value() << std::endl;
463
+ }
464
+ ```
465
+
466
+ — *end example*]
467
+
468
+ The coroutine state is destroyed when control flows off the end of the
469
+ coroutine or the `destroy` member function
470
+ [[coroutine.handle.resumption]] of a coroutine handle
471
+ [[coroutine.handle]] that refers to the coroutine is invoked. In the
472
+ latter case objects with automatic storage duration that are in scope at
473
+ the suspend point are destroyed in the reverse order of the
474
+ construction. The storage for the coroutine state is released by calling
475
+ a non-array deallocation function [[basic.stc.dynamic.deallocation]]. If
476
+ `destroy` is called for a coroutine that is not suspended, the program
477
+ has undefined behavior.
478
+
479
+ The deallocation function’s name is looked up in the scope of the
480
+ promise type. If this lookup fails, the deallocation function’s name is
481
+ looked up in the global scope. If deallocation function lookup finds
482
+ both a usual deallocation function with only a pointer parameter and a
483
+ usual deallocation function with both a pointer parameter and a size
484
+ parameter, then the selected deallocation function shall be the one with
485
+ two parameters. Otherwise, the selected deallocation function shall be
486
+ the function with one parameter. If no usual deallocation function is
487
+ found, the program is ill-formed. The selected deallocation function
488
+ shall be called with the address of the block of storage to be reclaimed
489
+ as its first argument. If a deallocation function with a parameter of
490
+ type `std::size_t` is used, the size of the block is passed as the
491
+ corresponding argument.
492
+
493
+ When a coroutine is invoked, after initializing its parameters
494
+ [[expr.call]], a copy is created for each coroutine parameter. For a
495
+ parameter of type cv `T`, the copy is a variable of type cv `T` with
496
+ automatic storage duration that is direct-initialized from an xvalue of
497
+ type `T` referring to the parameter.
498
+
499
+ [*Note 2*: An original parameter object is never a const or volatile
500
+ object [[basic.type.qualifier]]. — *end note*]
501
+
502
+ The initialization and destruction of each parameter copy occurs in the
503
+ context of the called coroutine. Initializations of parameter copies are
504
+ sequenced before the call to the coroutine promise constructor and
505
+ indeterminately sequenced with respect to each other. The lifetime of
506
+ parameter copies ends immediately after the lifetime of the coroutine
507
+ promise object ends.
508
+
509
+ [*Note 3*: If a coroutine has a parameter passed by reference, resuming
510
+ the coroutine after the lifetime of the entity referred to by that
511
+ parameter has ended is likely to result in undefined
512
+ behavior. — *end note*]
513
+
514
+ If the evaluation of the expression `promise.unhandled_exception()`
515
+ exits via an exception, the coroutine is considered suspended at the
516
+ final suspend point.
517
+
518
+ The expression `co_await` `promise.final_suspend()` shall not be
519
+ potentially-throwing [[except.spec]].
520
+