From Jason Turner

[stmt]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpj4w_bbzm/{from.md → to.md} +1345 -0
tmp/tmpj4w_bbzm/{from.md → to.md} RENAMED
@@ -0,0 +1,1345 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Statements <a id="stmt">[[stmt]]</a>
2
+
3
+ ## Preamble <a id="stmt.pre">[[stmt.pre]]</a>
4
+
5
+ Except as indicated, statements are executed in sequence
6
+ [[intro.execution]].
7
+
8
+ ``` bnf
9
+ statement:
10
+ labeled-statement
11
+ attribute-specifier-seqₒₚₜ expression-statement
12
+ attribute-specifier-seqₒₚₜ compound-statement
13
+ attribute-specifier-seqₒₚₜ selection-statement
14
+ attribute-specifier-seqₒₚₜ iteration-statement
15
+ attribute-specifier-seqₒₚₜ expansion-statement
16
+ attribute-specifier-seqₒₚₜ jump-statement
17
+ attribute-specifier-seqₒₚₜ assertion-statement
18
+ declaration-statement
19
+ attribute-specifier-seqₒₚₜ try-block
20
+ ```
21
+
22
+ ``` bnf
23
+ init-statement:
24
+ expression-statement
25
+ simple-declaration
26
+ alias-declaration
27
+ ```
28
+
29
+ ``` bnf
30
+ condition:
31
+ expression
32
+ attribute-specifier-seqₒₚₜ decl-specifier-seq declarator brace-or-equal-initializer
33
+ structured-binding-declaration initializer
34
+ ```
35
+
36
+ ``` bnf
37
+ for-range-declaration:
38
+ attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
39
+ structured-binding-declaration
40
+ ```
41
+
42
+ ``` bnf
43
+ for-range-initializer:
44
+ expr-or-braced-init-list
45
+ ```
46
+
47
+ The optional *attribute-specifier-seq* appertains to the respective
48
+ statement. See  [[dcl.meaning]] for the optional
49
+ *attribute-specifier-seq* in a *for-range-declaration*.
50
+
51
+ A *substatement* of a *statement* is one of the following:
52
+
53
+ - for a *labeled-statement*, its *statement*,
54
+ - for a *compound-statement*, any *statement* of its *statement-seq*,
55
+ - for a *selection-statement*, any of its *statement*s or
56
+ *compound-statement*s (but not its *init-statement*),
57
+ - for an *iteration-statement*, its *statement* (but not an
58
+ *init-statement*), or
59
+ - for an *expansion-statement*, its *compound-statement* (but not an
60
+ *init-statement*).
61
+
62
+ [*Note 1*: The *compound-statement* of a *lambda-expression* is not a
63
+ substatement of the *statement* (if any) in which the
64
+ *lambda-expression* lexically appears. — *end note*]
65
+
66
+ A *statement* `S1` *encloses* a *statement* `S2` if
67
+
68
+ - `S2` is a substatement of `S1`,
69
+ - `S1` is a *selection-statement*, *iteration-statement*, or
70
+ *expansion-statement*, and `S2` is the *init-statement* of `S1`,
71
+ - `S1` is a *try-block* and `S2` is its *compound-statement* or any of
72
+ the *compound-statement*s of its *handler*s, or
73
+ - `S1` encloses a statement `S3` and `S3` encloses `S2`.
74
+
75
+ A statement `S1` is *enclosed by* a statement `S2` if `S2` encloses
76
+ `S1`.
77
+
78
+ The rules for *condition*s apply both to *selection-statement*s
79
+ [[stmt.select]] and to the `for` and `while` statements [[stmt.iter]].
80
+ If a *structured-binding-declaration* appears in a *condition*, the
81
+ *condition* is a structured binding declaration [[dcl.pre]]. A
82
+ *condition* that is neither an *expression* nor a structured binding
83
+ declaration is a declaration [[dcl]]. The *declarator* shall not specify
84
+ a function or an array. The *decl-specifier-seq* shall not define a
85
+ class or enumeration. If the `auto` *type-specifier* appears in the
86
+ *decl-specifier-seq*, the type of the identifier being declared is
87
+ deduced from the initializer as described in  [[dcl.spec.auto]].
88
+
89
+ The *decision variable* of a *condition* that is neither an *expression*
90
+ nor a structured binding declaration is the declared variable. The
91
+ decision variable of a *condition* that is a structured binding
92
+ declaration is specified in [[dcl.struct.bind]].
93
+
94
+ The value of a *condition* that is not an *expression* in a statement
95
+ other than a `switch` statement is the value of the decision variable
96
+ contextually converted to `bool` [[conv]]. If that conversion is
97
+ ill-formed, the program is ill-formed. The value of a *condition* that
98
+ is an expression is the value of the expression, contextually converted
99
+ to `bool` for statements other than `switch`; if that conversion is
100
+ ill-formed, the program is ill-formed. The value of the condition will
101
+ be referred to as simply “the condition” where the usage is unambiguous.
102
+
103
+ If a *condition* can be syntactically resolved as either an expression
104
+ or a declaration, it is interpreted as the latter.
105
+
106
+ In the *decl-specifier-seq* of a *condition* or of a
107
+ *for-range-declaration*, including that of any
108
+ *structured-binding-declaration* of the *condition*, each
109
+ *decl-specifier* shall be either a *type-specifier* or `constexpr`. The
110
+ *decl-specifier-seq* of a *for-range-declaration* shall not define a
111
+ class or enumeration.
112
+
113
+ ## Label <a id="stmt.label">[[stmt.label]]</a>
114
+
115
+ A label can be added to a statement or used anywhere in a
116
+ *compound-statement*.
117
+
118
+ ``` bnf
119
+ label:
120
+ attribute-specifier-seqₒₚₜ identifier ':'
121
+ attribute-specifier-seqₒₚₜ case constant-expression ':'
122
+ attribute-specifier-seqₒₚₜ default ':'
123
+ ```
124
+
125
+ ``` bnf
126
+ labeled-statement:
127
+ label statement
128
+ ```
129
+
130
+ The optional *attribute-specifier-seq* appertains to the label. The only
131
+ use of a label with an *identifier* is as the target of a `goto`. No two
132
+ labels in a function shall have the same *identifier*. A label can be
133
+ used in a `goto` statement before its introduction.
134
+
135
+ A *labeled-statement* whose *label* is a `case` or `default` label shall
136
+ be enclosed by [[stmt.pre]] a `switch` statement [[stmt.switch]].
137
+
138
+ A *control-flow-limited statement* is a statement `S` for which:
139
+
140
+ - a `case` or `default` label appearing within `S` shall be associated
141
+ with a `switch` statement [[stmt.switch]] within `S`, and
142
+ - a label declared in `S` shall only be referred to by a statement
143
+ [[stmt.goto]] in `S`.
144
+
145
+ An identifier label shall not be enclosed by an *expansion-statement*
146
+ [[stmt.expand]].
147
+
148
+ ## Expression statement <a id="stmt.expr">[[stmt.expr]]</a>
149
+
150
+ Expression statements have the form
151
+
152
+ ``` bnf
153
+ expression-statement:
154
+ expressionₒₚₜ ';'
155
+ ```
156
+
157
+ The expression is a discarded-value expression [[expr.context]]. All
158
+ side effects from an expression statement are completed before the next
159
+ statement is executed. An expression statement with the *expression*
160
+ missing is called a *null statement*.
161
+
162
+ [*Note 1*: Most statements are expression statements — usually
163
+ assignments or function calls. A null statement is useful to supply a
164
+ null body to an iteration statement such as a `while` statement
165
+ [[stmt.while]]. — *end note*]
166
+
167
+ ## Compound statement or block <a id="stmt.block">[[stmt.block]]</a>
168
+
169
+ A *compound statement* (also known as a block) groups a sequence of
170
+ statements into a single statement.
171
+
172
+ ``` bnf
173
+ compound-statement:
174
+ '{' statement-seqₒₚₜ label-seqₒₚₜ '}'
175
+ ```
176
+
177
+ ``` bnf
178
+ statement-seq:
179
+ statement statement-seqₒₚₜ
180
+ ```
181
+
182
+ ``` bnf
183
+ label-seq:
184
+ label label-seqₒₚₜ
185
+ ```
186
+
187
+ A label at the end of a *compound-statement* is treated as if it were
188
+ followed by a null statement.
189
+
190
+ [*Note 1*: A compound statement defines a block scope [[basic.scope]].
191
+ A declaration is a *statement* [[stmt.dcl]]. — *end note*]
192
+
193
+ ## Selection statements <a id="stmt.select">[[stmt.select]]</a>
194
+
195
+ ### General <a id="stmt.select.general">[[stmt.select.general]]</a>
196
+
197
+ Selection statements choose one of several flows of control.
198
+
199
+ ``` bnf
200
+ selection-statement:
201
+ if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement
202
+ if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement else statement
203
+ if '!'ₒₚₜ consteval compound-statement
204
+ if '!'ₒₚₜ consteval compound-statement else statement
205
+ switch '(' init-statementₒₚₜ condition ')' statement
206
+ ```
207
+
208
+ See  [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
209
+ condition.
210
+
211
+ [*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
212
+
213
+ [*Note 2*: Each *selection-statement* and each substatement of a
214
+ *selection-statement* has a block scope
215
+ [[basic.scope.block]]. — *end note*]
216
+
217
+ ### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
218
+
219
+ If the condition [[stmt.pre]] yields `true`, the first substatement is
220
+ executed. If the `else` part of the selection statement is present and
221
+ the condition yields `false`, the second substatement is executed. If
222
+ the first substatement is reached via a label, the condition is not
223
+ evaluated and the second substatement is not executed. In the second
224
+ form of `if` statement (the one including `else`), if the first
225
+ substatement is also an `if` statement then that inner `if` statement
226
+ shall contain an `else` part.[^1]
227
+
228
+ If the `if` statement is of the form `if constexpr`, the value of the
229
+ condition is contextually converted to `bool` and the converted
230
+ expression shall be a constant expression [[expr.const]]; this form is
231
+ called a *constexpr if* statement. If the value of the converted
232
+ condition is `false`, the first substatement is a *discarded statement*,
233
+ otherwise the second substatement, if present, is a discarded statement.
234
+ During the instantiation of an enclosing templated entity [[temp.pre]],
235
+ if the condition is not value-dependent after its instantiation, the
236
+ discarded substatement (if any) is not instantiated. Each substatement
237
+ of a constexpr if statement is a control-flow-limited statement
238
+ [[stmt.label]].
239
+
240
+ [*Example 1*:
241
+
242
+ ``` cpp
243
+ if constexpr (sizeof(int[2])) {} // OK, narrowing allowed
244
+ ```
245
+
246
+ — *end example*]
247
+
248
+ [*Note 1*: Odr-uses [[term.odr.use]] in a discarded statement do not
249
+ require an entity to be defined. — *end note*]
250
+
251
+ [*Example 2*:
252
+
253
+ ``` cpp
254
+ template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
255
+ // ... handle p
256
+
257
+ if constexpr (sizeof...(rs) > 0)
258
+ g(rs...); // never instantiated with an empty argument list
259
+ }
260
+
261
+ extern int x; // no definition of x required
262
+
263
+ int f() {
264
+ if constexpr (true)
265
+ return 0;
266
+ else if (x)
267
+ return x;
268
+ else
269
+ return -x;
270
+ }
271
+ ```
272
+
273
+ — *end example*]
274
+
275
+ An `if` statement of the form
276
+
277
+ ``` bnf
278
+ if constexprₒₚₜ '(' init-statement condition ')' statement
279
+ ```
280
+
281
+ is equivalent to
282
+
283
+ ``` bnf
284
+ '{'
285
+ init-statement
286
+ if constexprₒₚₜ '(' condition ')' statement
287
+ '}'
288
+ ```
289
+
290
+ and an `if` statement of the form
291
+
292
+ ``` bnf
293
+ if constexprₒₚₜ '(' init-statement condition ')' statement else statement
294
+ ```
295
+
296
+ is equivalent to
297
+
298
+ ``` bnf
299
+ '{'
300
+ init-statement
301
+ if constexprₒₚₜ '(' condition ')' statement else statement
302
+ '}'
303
+ ```
304
+
305
+ except that the *init-statement* is in the same scope as the
306
+ *condition*.
307
+
308
+ An `if` statement of the form `if consteval` is called a
309
+ *consteval if statement*. The *statement*, if any, in a consteval if
310
+ statement shall be a *compound-statement*.
311
+
312
+ [*Example 3*:
313
+
314
+ ``` cpp
315
+ constexpr void f(bool b) {
316
+ if (true)
317
+ if consteval { }
318
+ else ; // error: not a compound-statement; else not associated with outer if
319
+ }
320
+ ```
321
+
322
+ — *end example*]
323
+
324
+ If a consteval if statement is evaluated in a context that is manifestly
325
+ constant-evaluated [[expr.const]], the first substatement is executed.
326
+
327
+ [*Note 2*: The first substatement is an immediate function
328
+ context. — *end note*]
329
+
330
+ Otherwise, if the `else` part of the selection statement is present,
331
+ then the second substatement is executed. Each substatement of a
332
+ consteval if statement is a control-flow-limited statement
333
+ [[stmt.label]].
334
+
335
+ An `if` statement of the form
336
+
337
+ ``` bnf
338
+ if '!' consteval compound-statement
339
+ ```
340
+
341
+ is not itself a consteval if statement, but is equivalent to the
342
+ consteval if statement
343
+
344
+ ``` bnf
345
+ if consteval '{' '}' else compound-statement
346
+ ```
347
+
348
+ An `if` statement of the form
349
+
350
+ ``` bnf
351
+ if '!' consteval compound-statement₁ else statement₂
352
+ ```
353
+
354
+ is not itself a consteval if statement, but is equivalent to the
355
+ consteval if statement
356
+
357
+ ``` bnf
358
+ if consteval statement₂ else compound-statement₁
359
+ ```
360
+
361
+ ### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
362
+
363
+ The `switch` statement causes control to be transferred to one of
364
+ several statements depending on the value of a condition.
365
+
366
+ If the *condition* is an *expression*, the value of the condition is the
367
+ value of the *expression*; otherwise, it is the value of the decision
368
+ variable. The value of the condition shall be of integral type,
369
+ enumeration type, or class type. If of class type, the condition is
370
+ contextually implicitly converted [[conv]] to an integral or enumeration
371
+ type. If the (possibly converted) type is subject to integral promotions
372
+ [[conv.prom]], the condition is converted to the promoted type. Any
373
+ statement within the `switch` statement can be labeled with one or more
374
+ case labels as follows:
375
+
376
+ ``` bnf
377
+ case constant-expression ':'
378
+ ```
379
+
380
+ where the *constant-expression* shall be a converted constant expression
381
+ [[expr.const]] of the adjusted type of the switch condition. No two of
382
+ the case constants in the same switch shall have the same value after
383
+ conversion.
384
+
385
+ There shall be at most one label of the form
386
+
387
+ ``` cpp
388
+ default :
389
+ ```
390
+
391
+ within a `switch` statement.
392
+
393
+ Switch statements can be nested; a `case` or `default` label is
394
+ associated with the smallest switch enclosing it.
395
+
396
+ When the `switch` statement is executed, its condition is evaluated. If
397
+ one of the case constants has the same value as the condition, control
398
+ is passed to the statement following the matched case label. If no case
399
+ constant matches the condition, and if there is a `default` label,
400
+ control passes to the statement labeled by the default label. If no case
401
+ matches and if there is no `default` then none of the statements in the
402
+ switch is executed.
403
+
404
+ `case` and `default` labels in themselves do not alter the flow of
405
+ control, which continues unimpeded across such labels. To exit from a
406
+ switch, see `break`, [[stmt.break]].
407
+
408
+ [*Note 1*: Usually, the substatement that is the subject of a switch is
409
+ compound and `case` and `default` labels appear on the top-level
410
+ statements contained within the (compound) substatement, but this is not
411
+ required. Declarations can appear in the substatement of a `switch`
412
+ statement. — *end note*]
413
+
414
+ A `switch` statement of the form
415
+
416
+ ``` bnf
417
+ switch '(' init-statement condition ')' statement
418
+ ```
419
+
420
+ is equivalent to
421
+
422
+ ``` bnf
423
+ '{'
424
+ init-statement
425
+ switch '(' condition ')' statement
426
+ '}'
427
+ ```
428
+
429
+ except that the *init-statement* is in the same scope as the
430
+ *condition*.
431
+
432
+ ## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
433
+
434
+ ### General <a id="stmt.iter.general">[[stmt.iter.general]]</a>
435
+
436
+ Iteration statements specify looping.
437
+
438
+ ``` bnf
439
+ iteration-statement:
440
+ while '(' condition ')' statement
441
+ do statement while '(' expression ')' ';'
442
+ for '(' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
443
+ for '(' init-statementₒₚₜ for-range-declaration ':' for-range-initializer ')' statement
444
+ ```
445
+
446
+ [*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
447
+
448
+ The substatement in an *iteration-statement* implicitly defines a block
449
+ scope [[basic.scope]] which is entered and exited each time through the
450
+ loop. If the substatement in an *iteration-statement* is a single
451
+ statement and not a *compound-statement*, it is as if it was rewritten
452
+ to be a *compound-statement* containing the original statement.
453
+
454
+ [*Example 1*:
455
+
456
+ ``` cpp
457
+ while (--x >= 0)
458
+ int i;
459
+ ```
460
+
461
+ can be equivalently rewritten as
462
+
463
+ ``` cpp
464
+ while (--x >= 0) {
465
+ int i;
466
+ }
467
+ ```
468
+
469
+ Thus after the `while` statement, `i` is no longer in scope.
470
+
471
+ — *end example*]
472
+
473
+ A *trivially empty iteration statement* is an iteration statement
474
+ matching one of the following forms:
475
+
476
+ - `while (` *expression* `) ;`
477
+ - `while (` *expression* `) { }`
478
+ - `do ; while (` *expression* `) ;`
479
+ - `do { } while (` *expression* `) ;`
480
+ - `for (` *init-statement* *expression*ₒₚₜ `; ) ;`
481
+ - `for (` *init-statement* *expression*ₒₚₜ `; ) { }`
482
+
483
+ The *controlling expression* of a trivially empty iteration statement is
484
+ the *expression* of a `while`, `do`, or `for` statement (or `true`, if
485
+ the `for` statement has no *expression*). A *trivial infinite loop* is a
486
+ trivially empty iteration statement for which the converted controlling
487
+ expression is a constant expression, when interpreted as a
488
+ *constant-expression* [[expr.const]], and evaluates to `true`. The
489
+ *statement* of a trivial infinite loop is replaced with a call to the
490
+ function `std::this_thread::yield` [[thread.thread.this]]; it is
491
+ *implementation-defined* whether this replacement occurs on freestanding
492
+ implementations.
493
+
494
+ [*Note 2*: In a freestanding environment, concurrent forward progress
495
+ is not guaranteed; such systems therefore require explicit cooperation.
496
+ A call to yield can add implicit cooperation where none is otherwise
497
+ intended. — *end note*]
498
+
499
+ ### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
500
+
501
+ In the `while` statement, the substatement is executed repeatedly until
502
+ the value of the condition [[stmt.pre]] becomes `false`. The test takes
503
+ place before each execution of the substatement.
504
+
505
+ A `while` statement is equivalent to
506
+
507
+ ``` bnf
508
+ label ':'
509
+ '{'
510
+ if '(' condition ')' '{'
511
+ statement
512
+ goto label ';'
513
+ '}'
514
+ '}'
515
+ ```
516
+
517
+ [*Note 1*:
518
+
519
+ The variable created in the condition is destroyed and created with each
520
+ iteration of the loop.
521
+
522
+ [*Example 1*:
523
+
524
+ ``` cpp
525
+ struct A {
526
+ int val;
527
+ A(int i) : val(i) { }
528
+ ~A() { }
529
+ operator bool() { return val != 0; }
530
+ };
531
+ int i = 1;
532
+ while (A a = i) {
533
+ // ...
534
+ i = 0;
535
+ }
536
+ ```
537
+
538
+ In the while-loop, the constructor and destructor are each called twice,
539
+ once for the condition that succeeds and once for the condition that
540
+ fails.
541
+
542
+ — *end example*]
543
+
544
+ — *end note*]
545
+
546
+ ### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
547
+
548
+ The expression is contextually converted to `bool` [[conv]]; if that
549
+ conversion is ill-formed, the program is ill-formed.
550
+
551
+ In the `do` statement, the substatement is executed repeatedly until the
552
+ value of the expression becomes `false`. The test takes place after each
553
+ execution of the statement.
554
+
555
+ ### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
556
+
557
+ The `for` statement
558
+
559
+ ``` bnf
560
+ for '(' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
561
+ ```
562
+
563
+ is equivalent to
564
+
565
+ ``` bnf
566
+ '{'
567
+ init-statement
568
+ while '(' condition ')' '{'
569
+ statement
570
+ expression ';'
571
+ '}'
572
+ '}'
573
+ ```
574
+
575
+ except that the *init-statement* is in the same scope as the
576
+ *condition*, and except that a `continue` in *statement* (not enclosed
577
+ in another iteration statement) will execute *expression* before
578
+ re-evaluating *condition*.
579
+
580
+ [*Note 1*: Thus the first statement specifies initialization for the
581
+ loop; the condition [[stmt.pre]] specifies a test, sequenced before each
582
+ iteration, such that the loop is exited when the condition becomes
583
+ `false`; the expression often specifies incrementing that is sequenced
584
+ after each iteration. — *end note*]
585
+
586
+ Either or both of the *condition* and the *expression* can be omitted. A
587
+ missing *condition* makes the implied `while` clause equivalent to
588
+ `while (true)`.
589
+
590
+ ### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
591
+
592
+ The range-based `for` statement
593
+
594
+ ``` bnf
595
+ for '(' init-statementₒₚₜ for-range-declaration ':' for-range-initializer ')' statement
596
+ ```
597
+
598
+ is equivalent to
599
+
600
+ ``` bnf
601
+ '{'
602
+ init-statementₒₚₜ
603
+ auto '&&'range '=' for-range-initializer ';'
604
+ auto begin '=' begin-expr ';'
605
+ auto end '=' end-expr ';'
606
+ for '(' ';' begin '!=' end';' '++'begin ')' '{'
607
+ for-range-declaration '=' '*' begin ';'
608
+ statement
609
+ '}'
610
+ '}'
611
+ ```
612
+
613
+ where
614
+
615
+ - if the *for-range-initializer* is an *expression*, it is regarded as
616
+ if it were surrounded by parentheses (so that a comma operator cannot
617
+ be reinterpreted as delimiting two *init-declarator*s);
618
+ - *`range`*, *`begin`*, and *`end`* are variables defined for exposition
619
+ only; and
620
+ - *`begin-expr`* and *`end-expr`* are determined as follows:
621
+ - if the type of *`range`* is a reference to an array type `R`,
622
+ *`begin-expr`* and *`end-expr`* are *`range`* and *`range`* `+` `N`,
623
+ respectively, where `N` is the array bound. If `R` is an array of
624
+ unknown bound or an array of incomplete type, the program is
625
+ ill-formed;
626
+ - if the type of *`range`* is a reference to a class type `C`, and
627
+ searches in the scope of `C` [[class.member.lookup]] for the names
628
+ `begin` and `end` each find at least one declaration, *`begin-expr`*
629
+ and *`end-expr`* are `range.begin()` and `range.end()`,
630
+ respectively;
631
+ - otherwise, *`begin-expr`* and *`end-expr`* are `begin(range)` and
632
+ `end(range)`, respectively, where `begin` and `end` undergo
633
+ argument-dependent lookup [[basic.lookup.argdep]].
634
+ \[*Note 1*: Ordinary unqualified lookup [[basic.lookup.unqual]] is
635
+ not performed. — *end note*]
636
+
637
+ [*Example 1*:
638
+
639
+ ``` cpp
640
+ int array[5] = { 1, 2, 3, 4, 5 };
641
+ for (int& x : array)
642
+ x *= 2;
643
+ ```
644
+
645
+ — *end example*]
646
+
647
+ [*Note 2*: The lifetime of some temporaries in the
648
+ *for-range-initializer* is extended to cover the entire loop
649
+ [[class.temporary]]. — *end note*]
650
+
651
+ [*Example 2*:
652
+
653
+ ``` cpp
654
+ using T = std::list<int>;
655
+ const T& f1(const T& t) { return t; }
656
+ const T& f2(T t) { return t; }
657
+ T g();
658
+
659
+ void foo() {
660
+ for (auto e : f1(g())) {} // OK, lifetime of return value of g() extended
661
+ for (auto e : f2(g())) {} // undefined behavior
662
+ }
663
+ ```
664
+
665
+ — *end example*]
666
+
667
+ ## Expansion statements <a id="stmt.expand">[[stmt.expand]]</a>
668
+
669
+ Expansion statements specify repeated instantiations
670
+ [[temp.decls.general]] of their substatement.
671
+
672
+ ``` bnf
673
+ expansion-statement:
674
+ template for '('
675
+ init-statementₒₚₜ for-range-declaration ':'
676
+ expansion-initializer ')' compound-statement
677
+ ```
678
+
679
+ ``` bnf
680
+ expansion-initializer:
681
+ expression
682
+ expansion-init-list
683
+ ```
684
+
685
+ ``` bnf
686
+ expansion-init-list:
687
+ '{' expression-listₒₚₜ '}'
688
+ ```
689
+
690
+ The *compound-statement* of an *expansion-statement* is a
691
+ control-flow-limited statement [[stmt.label]].
692
+
693
+ For an expression `E`, let the expressions *`begin-expr`* and
694
+ *`end-expr`* be determined as specified in  [[stmt.ranged]]. An
695
+ expression is *expansion-iterable* if it does not have array type and
696
+ either
697
+
698
+ - *`begin-expr`* and *`end-expr`* are of the form `E.begin()` and
699
+ `E.end()`, or
700
+ - argument-dependent lookups for `begin(E)` and for `end(E)` each find
701
+ at least one function or function template.
702
+
703
+ An expansion statement is
704
+
705
+ - an *enumerating expansion statement* if its *expansion-initializer* is
706
+ of the form *expansion-init-list*;
707
+ - otherwise, an *iterating expansion statement* if its
708
+ *expansion-initializer* is an expansion-iterable expression;
709
+ - otherwise, a *destructuring expansion statement*.
710
+
711
+ An expansion statement S is equivalent to a *compound-statement*
712
+ containing instantiations of the *for-range-declaration* (including its
713
+ implied initialization), together with the compound-statement of S, as
714
+ follows:
715
+
716
+ - If S is an enumerating expansion statement, S is equivalent to:
717
+ ``` cpp
718
+ {
719
+ init-statement
720
+ S₀
721
+
722
+ S_N-1}
723
+ }
724
+ ```
725
+
726
+ where N is the number of elements in the *expression-list*, Sᵢ is
727
+ ``` cpp
728
+ {
729
+ for-range-declaration = Eᵢ;
730
+ compound-statement
731
+ }
732
+ ```
733
+
734
+ and Eᵢ is the iᵗʰ element of the *expression-list*.
735
+ - Otherwise, if S is an iterating expansion statement, S is equivalent
736
+ to:
737
+ ``` cpp
738
+ {
739
+ init-statement
740
+ static constexpr auto&& range = expansion-initializer;
741
+ static constexpr auto begin = begin-expr; // see [stmt.ranged]
742
+ static constexpr auto end = end-expr; // see [stmt.ranged]
743
+
744
+ S₀
745
+
746
+ S_N-1}
747
+ }
748
+ ```
749
+
750
+ where N is the result of evaluating the expression
751
+ ``` cpp
752
+ []consteval {
753
+ std::ptrdiff_t result = 0;
754
+ for (auto i = begin; i != end; ++i, ++result);
755
+ return result; // distance from begin to end
756
+ }()
757
+ ```
758
+
759
+ and Sᵢ is
760
+ ``` cpp
761
+ {
762
+ static constexpr auto iter = begin + i;
763
+ for-range-declaration = *iter;
764
+ compound-statement
765
+ }
766
+ ```
767
+
768
+ The variables *`range`*, *`begin`*, *`end`*, and *`iter`* are defined
769
+ for exposition only.
770
+ \[*Note 1*: The instantiation is ill-formed if *`range`* is not a
771
+ constant expression [[expr.const]]. — *end note*]
772
+ - Otherwise, S is a destructuring expansion statement and S is
773
+ equivalent to:
774
+ ``` cpp
775
+ {
776
+ init-statement
777
+ \opt{constexpr} auto&& [u₀, u₁, …, u_N-1}] = expansion-initializer;
778
+ S₀
779
+
780
+ S_N-1}
781
+ }
782
+ ```
783
+
784
+ where N is the structured binding size of the type of the
785
+ *expansion-initializer* and Sᵢ is
786
+ ``` cpp
787
+ {
788
+ for-range-declaration = uᵢ;
789
+ compound-statement
790
+ }
791
+ ```
792
+
793
+ The keyword `constexpr` is present in the declaration of
794
+ $u_{0}, u_{1}, \dotsc, u_{N-1}$ if and only if `constexpr` is one of
795
+ the *decl-specifier*s of the *decl-specifier-seq* of the
796
+ *for-range-declaration*.
797
+
798
+ [*Example 1*:
799
+
800
+ ``` cpp
801
+ consteval int f(auto const&... Containers) {
802
+ int result = 0;
803
+ template for (auto const& c : {Containers...}) { // OK, enumerating expansion statement
804
+ result += c[0];
805
+ }
806
+ return result;
807
+ }
808
+ constexpr int c1[] = {1, 2, 3};
809
+ constexpr int c2[] = {4, 3, 2, 1};
810
+ static_assert(f(c1, c2) == 5);
811
+ ```
812
+
813
+ — *end example*]
814
+
815
+ [*Example 2*:
816
+
817
+ ``` cpp
818
+ consteval int f() {
819
+ constexpr std::array<int, 3> arr {1, 2, 3};
820
+ int result = 0;
821
+ template for (constexpr int s : arr) { // OK, iterating expansion statement
822
+ result += sizeof(char[s]);
823
+ }
824
+ return result;
825
+ }
826
+ static_assert(f() == 6);
827
+ ```
828
+
829
+ — *end example*]
830
+
831
+ [*Example 3*:
832
+
833
+ ``` cpp
834
+ struct S {
835
+ int i;
836
+ short s;
837
+ };
838
+
839
+ consteval long f(S s) {
840
+ long result = 0;
841
+ template for (auto x : s) { // OK, destructuring expansion statement
842
+ result += sizeof(x);
843
+ }
844
+ return result;
845
+ }
846
+ static_assert(f(S{}) == sizeof(int) + sizeof(short));
847
+ ```
848
+
849
+ — *end example*]
850
+
851
+ ## Jump statements <a id="stmt.jump">[[stmt.jump]]</a>
852
+
853
+ ### General <a id="stmt.jump.general">[[stmt.jump.general]]</a>
854
+
855
+ Jump statements unconditionally transfer control.
856
+
857
+ ``` bnf
858
+ jump-statement:
859
+ break ';'
860
+ continue ';'
861
+ return expr-or-braced-init-listₒₚₜ ';'
862
+ coroutine-return-statement
863
+ goto identifier ';'
864
+ ```
865
+
866
+ [*Note 1*: On exit from a scope (however accomplished), objects with
867
+ automatic storage duration [[basic.stc.auto]] that have been constructed
868
+ in that scope are destroyed in the reverse order of their construction
869
+ [[stmt.dcl]]. For temporaries, see  [[class.temporary]]. However, the
870
+ program can be terminated (by calling `std::exit()` or `std::abort()`
871
+ [[support.start.term]], for example) without destroying objects with
872
+ automatic storage duration. — *end note*]
873
+
874
+ [*Note 2*: A suspension of a coroutine [[expr.await]] is not considered
875
+ to be an exit from a scope. — *end note*]
876
+
877
+ ### The `break` statement <a id="stmt.break">[[stmt.break]]</a>
878
+
879
+ A `break` statement shall be enclosed by [[stmt.pre]] an
880
+ *iteration-statement* [[stmt.iter]], an *expansion-statement*
881
+ [[stmt.expand]], or a `switch` statement [[stmt.switch]]. The `break`
882
+ statement causes termination of the innermost such enclosing statement;
883
+ control passes to the statement following the terminated statement, if
884
+ any.
885
+
886
+ ### The `continue` statement <a id="stmt.cont">[[stmt.cont]]</a>
887
+
888
+ A `continue` statement shall be enclosed by [[stmt.pre]] an
889
+ *iteration-statement* or an *expansion-statement*. If the innermost
890
+ enclosing such statement X is an *iteration-statement* [[stmt.iter]],
891
+ the `continue` statement causes control to pass to the end of the
892
+ *statement* or *compound-statement* of X. Otherwise, control passes to
893
+ the end of the *compound-statement* of the current Sᵢ [[stmt.expand]].
894
+
895
+ ### The `return` statement <a id="stmt.return">[[stmt.return]]</a>
896
+
897
+ A function returns control to its caller by the `return` statement.
898
+
899
+ The *expr-or-braced-init-list* of a `return` statement is called its
900
+ operand. A `return` statement with no operand shall be used only in a
901
+ function whose return type is cv `void`, a constructor [[class.ctor]],
902
+ or a destructor [[class.dtor]]. A `return` statement with an operand of
903
+ type `void` shall be used only in a function that has a cv `void` return
904
+ type. A `return` statement with any other operand shall be used only in
905
+ a function that has a return type other than cv `void`; the `return`
906
+ statement initializes the returned reference or prvalue result object of
907
+ the (explicit or implicit) function call by copy-initialization
908
+ [[dcl.init]] from the operand.
909
+
910
+ [*Note 1*: A constructor or destructor does not have a return
911
+ type. — *end note*]
912
+
913
+ [*Note 2*: A `return` statement can involve an invocation of a
914
+ constructor to perform a copy or move of the operand if it is not a
915
+ prvalue or if its type differs from the return type of the function. A
916
+ copy operation associated with a `return` statement can be elided or
917
+ converted to a move operation if an automatic storage duration variable
918
+ is returned [[class.copy.elision]]. — *end note*]
919
+
920
+ The destructor for the result object is potentially invoked
921
+ [[class.dtor]], [[except.ctor]].
922
+
923
+ [*Example 1*:
924
+
925
+ ``` cpp
926
+ class A {
927
+ ~A() {}
928
+ };
929
+ A f() { return A(); } // error: destructor of A is private (even though it is never invoked)
930
+ ```
931
+
932
+ — *end example*]
933
+
934
+ Flowing off the end of a constructor, a destructor, or a non-coroutine
935
+ function with a cv `void` return type is equivalent to a `return` with
936
+ no operand. Otherwise, flowing off the end of a function that is neither
937
+ `main` [[basic.start.main]] nor a coroutine [[dcl.fct.def.coroutine]]
938
+ results in undefined behavior.
939
+
940
+ The copy-initialization of the result of the call is sequenced before
941
+ the destruction of temporaries at the end of the full-expression
942
+ established by the operand of the `return` statement, which, in turn, is
943
+ sequenced before the destruction of local variables [[stmt.jump]] of the
944
+ block enclosing the `return` statement.
945
+
946
+ [*Note 3*: These operations are sequenced before the destruction of
947
+ local variables in each remaining enclosing block of the function
948
+ [[stmt.dcl]], which, in turn, is sequenced before the evaluation of
949
+ postcondition assertions of the function [[dcl.contract.func]], which,
950
+ in turn, is sequenced before the destruction of function parameters
951
+ [[expr.call]]. — *end note*]
952
+
953
+ In a function whose return type is a reference, other than an invented
954
+ function for `std::is_convertible` [[meta.rel]], a `return` statement
955
+ that binds the returned reference to a temporary expression
956
+ [[class.temporary]] is ill-formed.
957
+
958
+ [*Example 2*:
959
+
960
+ ``` cpp
961
+ auto&& f1() {
962
+ return 42; // ill-formed
963
+ }
964
+ const double& f2() {
965
+ static int x = 42;
966
+ return x; // ill-formed
967
+ }
968
+ auto&& id(auto&& r) {
969
+ return static_cast<decltype(r)&&>(r);
970
+ }
971
+ auto&& f3() {
972
+ return id(42); // OK, but probably a bug
973
+ }
974
+ ```
975
+
976
+ — *end example*]
977
+
978
+ ### The `co_return` statement <a id="stmt.return.coroutine">[[stmt.return.coroutine]]</a>
979
+
980
+ ``` bnf
981
+ coroutine-return-statement:
982
+ co_return expr-or-braced-init-listₒₚₜ ';'
983
+ ```
984
+
985
+ A `co_return` statement transfers control to the caller or resumer of a
986
+ coroutine [[dcl.fct.def.coroutine]]. A coroutine shall not enclose a
987
+ `return` statement [[stmt.return]].
988
+
989
+ [*Note 1*: For this determination, it is irrelevant whether the
990
+ `return` statement is enclosed by a discarded statement
991
+ [[stmt.if]]. — *end note*]
992
+
993
+ The *expr-or-braced-init-list* of a `co_return` statement is called its
994
+ operand. Let *p* be an lvalue naming the coroutine promise object
995
+ [[dcl.fct.def.coroutine]]. A `co_return` statement is equivalent to:
996
+
997
+ ``` bnf
998
+ '{' S';' goto final-suspend';' '}'
999
+ ```
1000
+
1001
+ where *`final-suspend`* is the exposition-only label defined in
1002
+ [[dcl.fct.def.coroutine]] and *S* is defined as follows:
1003
+
1004
+ - If the operand is a *braced-init-list* or an expression of non-`void`
1005
+ type, *S* is *p*`.return_value(`*expr-or-braced-init-list*`)`. The
1006
+ expression *S* shall be a prvalue of type `void`.
1007
+ - Otherwise, *S* is the *compound-statement* `{` *expression*ₒₚₜ `;`
1008
+ *p*`.return_void()``; }`. The expression *p*`.return_void()` shall be
1009
+ a prvalue of type `void`.
1010
+
1011
+ If a search for the name `return_void` in the scope of the promise type
1012
+ finds any declarations, flowing off the end of a coroutine’s
1013
+ *function-body* is equivalent to a `co_return` with no operand;
1014
+ otherwise flowing off the end of a coroutine’s *function-body* results
1015
+ in undefined behavior.
1016
+
1017
+ ### The `goto` statement <a id="stmt.goto">[[stmt.goto]]</a>
1018
+
1019
+ The `goto` statement unconditionally transfers control to the statement
1020
+ labeled by the identifier. The identifier shall be a label
1021
+ [[stmt.label]] located in the current function.
1022
+
1023
+ ## Assertion statement <a id="stmt.contract.assert">[[stmt.contract.assert]]</a>
1024
+
1025
+ ``` bnf
1026
+ assertion-statement:
1027
+ 'contract_assert' attribute-specifier-seqₒₚₜ '(' conditional-expression ')' ';'
1028
+ ```
1029
+
1030
+ An *assertion-statement* introduces a contract assertion
1031
+ [[basic.contract]]. The optional *attribute-specifier-seq* appertains to
1032
+ the introduced contract assertion.
1033
+
1034
+ The predicate [[basic.contract.general]] of an *assertion-statement* is
1035
+ its *conditional-expression* contextually converted to `bool`.
1036
+
1037
+ The evaluation of consecutive *assertion-statement*s is an evaluation in
1038
+ sequence [[basic.contract.eval]] of the contract assertions introduced
1039
+ by those *assertion-statement*s.
1040
+
1041
+ [*Note 1*:
1042
+
1043
+ A sequence of *assertion-statement*s can thus be repeatedly evaluated as
1044
+ a group.
1045
+
1046
+ [*Example 1*:
1047
+
1048
+ ``` cpp
1049
+ int f(int i)
1050
+ {
1051
+ contract_assert(i == 0); // #1
1052
+ contract_assert(i >= 0); // #2
1053
+ return 0;
1054
+ }
1055
+ int g = f(0); // can evaluate #1, #2, #1, #2
1056
+ ```
1057
+
1058
+ — *end example*]
1059
+
1060
+ — *end note*]
1061
+
1062
+ ## Declaration statement <a id="stmt.dcl">[[stmt.dcl]]</a>
1063
+
1064
+ A declaration statement introduces one or more new names into a block;
1065
+ it has the form
1066
+
1067
+ ``` bnf
1068
+ declaration-statement:
1069
+ block-declaration
1070
+ ```
1071
+
1072
+ [*Note 1*: If an identifier introduced by a declaration was previously
1073
+ declared in an outer block, the outer declaration is hidden for the
1074
+ remainder of the block [[basic.lookup.unqual]], after which it resumes
1075
+ its force. — *end note*]
1076
+
1077
+ A block variable with automatic storage duration [[basic.stc.auto]] is
1078
+ *active* everywhere in the scope to which it belongs after its
1079
+ *init-declarator*. Upon each transfer of control (including sequential
1080
+ execution of statements) within a function from point P to point Q, all
1081
+ block variables with automatic storage duration that are active at P and
1082
+ not at Q are destroyed in the reverse order of their construction. Then,
1083
+ all block variables with automatic storage duration that are active at Q
1084
+ but not at P are initialized in declaration order; unless all such
1085
+ variables have vacuous initialization [[basic.life]], the transfer of
1086
+ control shall not be a jump.[^2]
1087
+
1088
+ When a *declaration-statement* is executed, P and Q are the points
1089
+ immediately before and after it; when a function returns, Q is after its
1090
+ body.
1091
+
1092
+ [*Example 1*:
1093
+
1094
+ ``` cpp
1095
+ void f() {
1096
+ // ...
1097
+ goto lx; // error: jump into scope of a
1098
+ // ...
1099
+ ly:
1100
+ X a = 1;
1101
+ // ...
1102
+ lx:
1103
+ goto ly; // OK, jump implies destructor call for a followed by
1104
+ // construction again immediately following label ly
1105
+ }
1106
+ ```
1107
+
1108
+ — *end example*]
1109
+
1110
+ Dynamic initialization of a block variable with static storage duration
1111
+ [[basic.stc.static]] or thread storage duration [[basic.stc.thread]] is
1112
+ performed the first time control passes through its declaration; such a
1113
+ variable is considered initialized upon the completion of its
1114
+ initialization. If the initialization exits by throwing an exception,
1115
+ the initialization is not complete, so it will be tried again the next
1116
+ time control enters the declaration. If control enters the declaration
1117
+ concurrently while the variable is being initialized, the concurrent
1118
+ execution shall wait for completion of the initialization.
1119
+
1120
+ [*Note 2*: A conforming implementation cannot introduce any deadlock
1121
+ around execution of the initializer. Deadlocks might still be caused by
1122
+ the program logic; the implementation need only avoid deadlocks due to
1123
+ its own synchronization operations. — *end note*]
1124
+
1125
+ If control re-enters the declaration recursively while the variable is
1126
+ being initialized, the behavior is undefined.
1127
+
1128
+ [*Example 2*:
1129
+
1130
+ ``` cpp
1131
+ int foo(int i) {
1132
+ static int s = foo(2*i); // undefined behavior: recursive call
1133
+ return i+1;
1134
+ }
1135
+ ```
1136
+
1137
+ — *end example*]
1138
+
1139
+ An object associated with a block variable with static or thread storage
1140
+ duration will be destroyed if and only if it was constructed.
1141
+
1142
+ [*Note 3*: [[basic.start.term]] describes the order in which such
1143
+ objects are destroyed. — *end note*]
1144
+
1145
+ ## Ambiguity resolution <a id="stmt.ambig">[[stmt.ambig]]</a>
1146
+
1147
+ There is an ambiguity in the grammar involving *expression-statement*s
1148
+ and *declaration*s: An *expression-statement* with a function-style
1149
+ explicit type conversion [[expr.type.conv]] as its leftmost
1150
+ subexpression can be indistinguishable from a *declaration* where the
1151
+ first *declarator* starts with a `(`. In those cases the *statement* is
1152
+ considered a *declaration*, except as specified below.
1153
+
1154
+ [*Note 1*:
1155
+
1156
+ If the *statement* cannot syntactically be a *declaration*, there is no
1157
+ ambiguity, so this rule does not apply. In some cases, the whole
1158
+ *statement* needs to be examined to determine whether this is the case.
1159
+ This resolves the meaning of many examples.
1160
+
1161
+ [*Example 1*:
1162
+
1163
+ Assuming `T` is a *simple-type-specifier* [[dcl.type.simple]],
1164
+
1165
+ ``` cpp
1166
+ T(a)->m = 7; // expression-statement
1167
+ T(a)++; // expression-statement
1168
+ T(a,5)<<c; // expression-statement
1169
+
1170
+ T(*d)(int); // declaration
1171
+ T(e)[5]; // declaration
1172
+ T(f) = { 1, 2 }; // declaration
1173
+ T(*g)(double(3)); // declaration
1174
+ ```
1175
+
1176
+ In the last example above, `g`, which is a pointer to `T`, is
1177
+ initialized to `double(3)`. This is of course ill-formed for semantic
1178
+ reasons, but that does not affect the syntactic analysis.
1179
+
1180
+ — *end example*]
1181
+
1182
+ The remaining cases are *declaration*s.
1183
+
1184
+ [*Example 2*:
1185
+
1186
+ ``` cpp
1187
+ class T {
1188
+ // ...
1189
+ public:
1190
+ T();
1191
+ T(int);
1192
+ T(int, int);
1193
+ };
1194
+ T(a); // declaration
1195
+ T(*b)(); // declaration
1196
+ T(c)=7; // declaration
1197
+ T(d),e,f=3; // declaration
1198
+ extern int h;
1199
+ T(g)(h,2); // declaration
1200
+ ```
1201
+
1202
+ — *end example*]
1203
+
1204
+ — *end note*]
1205
+
1206
+ The disambiguation is purely syntactic; that is, the meaning of the
1207
+ names occurring in such a statement, beyond whether they are
1208
+ *type-name*s or not, is not generally used in or changed by the
1209
+ disambiguation. Class templates are instantiated as necessary to
1210
+ determine if a qualified name is a *type-name*. Disambiguation precedes
1211
+ parsing, and a statement disambiguated as a declaration may be an
1212
+ ill-formed declaration. If, during parsing, lookup finds that a name in
1213
+ a template argument is bound to (part of) the declaration being parsed,
1214
+ the program is ill-formed. No diagnostic is required.
1215
+
1216
+ [*Example 3*:
1217
+
1218
+ ``` cpp
1219
+ struct T1 {
1220
+ T1 operator()(int x) { return T1(x); }
1221
+ int operator=(int x) { return x; }
1222
+ T1(int) { }
1223
+ };
1224
+ struct T2 { T2(int) { } };
1225
+ int a, (*(*b)(T2))(int), c, d;
1226
+
1227
+ void f() {
1228
+ // disambiguation requires this to be parsed as a declaration:
1229
+ T1(a) = 3,
1230
+ T2(4), // T2 will be declared as a variable of type T1, but this will not
1231
+ (*(*b)(T2(c)))(int(d)); // allow the last part of the declaration to parse properly,
1232
+ // since it depends on T2 being a type-name
1233
+ }
1234
+ ```
1235
+
1236
+ — *end example*]
1237
+
1238
+ A syntactically ambiguous statement that can syntactically be a
1239
+ *declaration* with an outermost *declarator* with a
1240
+ *trailing-return-type* is considered a *declaration* only if it starts
1241
+ with `auto`.
1242
+
1243
+ [*Example 4*:
1244
+
1245
+ ``` cpp
1246
+ struct M;
1247
+ struct S {
1248
+ S* operator()();
1249
+ int N;
1250
+ int M;
1251
+
1252
+ void mem(S s) {
1253
+ auto(s)()->M; // expression, S::M hides ::M
1254
+ }
1255
+ };
1256
+
1257
+ void f(S s) {
1258
+ {
1259
+ auto(s)()->N; // expression
1260
+ auto(s)()->M; // function declaration
1261
+ }
1262
+ {
1263
+ S(s)()->N; // expression
1264
+ S(s)()->M; // expression
1265
+ }
1266
+ }
1267
+ ```
1268
+
1269
+ — *end example*]
1270
+
1271
+ <!-- Link reference definitions -->
1272
+ [basic.contract]: basic.md#basic.contract
1273
+ [basic.contract.eval]: basic.md#basic.contract.eval
1274
+ [basic.contract.general]: basic.md#basic.contract.general
1275
+ [basic.life]: basic.md#basic.life
1276
+ [basic.lookup.argdep]: basic.md#basic.lookup.argdep
1277
+ [basic.lookup.unqual]: basic.md#basic.lookup.unqual
1278
+ [basic.scope]: basic.md#basic.scope
1279
+ [basic.scope.block]: basic.md#basic.scope.block
1280
+ [basic.start.main]: basic.md#basic.start.main
1281
+ [basic.start.term]: basic.md#basic.start.term
1282
+ [basic.stc.auto]: basic.md#basic.stc.auto
1283
+ [basic.stc.static]: basic.md#basic.stc.static
1284
+ [basic.stc.thread]: basic.md#basic.stc.thread
1285
+ [class.copy.elision]: class.md#class.copy.elision
1286
+ [class.ctor]: class.md#class.ctor
1287
+ [class.dtor]: class.md#class.dtor
1288
+ [class.member.lookup]: basic.md#class.member.lookup
1289
+ [class.temporary]: basic.md#class.temporary
1290
+ [conv]: expr.md#conv
1291
+ [conv.prom]: expr.md#conv.prom
1292
+ [dcl]: dcl.md#dcl
1293
+ [dcl.contract.func]: dcl.md#dcl.contract.func
1294
+ [dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
1295
+ [dcl.init]: dcl.md#dcl.init
1296
+ [dcl.meaning]: dcl.md#dcl.meaning
1297
+ [dcl.pre]: dcl.md#dcl.pre
1298
+ [dcl.spec.auto]: dcl.md#dcl.spec.auto
1299
+ [dcl.struct.bind]: dcl.md#dcl.struct.bind
1300
+ [dcl.type.simple]: dcl.md#dcl.type.simple
1301
+ [except.ctor]: except.md#except.ctor
1302
+ [expr.await]: expr.md#expr.await
1303
+ [expr.call]: expr.md#expr.call
1304
+ [expr.const]: expr.md#expr.const
1305
+ [expr.context]: expr.md#expr.context
1306
+ [expr.type.conv]: expr.md#expr.type.conv
1307
+ [intro.execution]: basic.md#intro.execution
1308
+ [meta.rel]: meta.md#meta.rel
1309
+ [stmt]: #stmt
1310
+ [stmt.ambig]: #stmt.ambig
1311
+ [stmt.block]: #stmt.block
1312
+ [stmt.break]: #stmt.break
1313
+ [stmt.cont]: #stmt.cont
1314
+ [stmt.contract.assert]: #stmt.contract.assert
1315
+ [stmt.dcl]: #stmt.dcl
1316
+ [stmt.do]: #stmt.do
1317
+ [stmt.expand]: #stmt.expand
1318
+ [stmt.expr]: #stmt.expr
1319
+ [stmt.for]: #stmt.for
1320
+ [stmt.goto]: #stmt.goto
1321
+ [stmt.if]: #stmt.if
1322
+ [stmt.iter]: #stmt.iter
1323
+ [stmt.iter.general]: #stmt.iter.general
1324
+ [stmt.jump]: #stmt.jump
1325
+ [stmt.jump.general]: #stmt.jump.general
1326
+ [stmt.label]: #stmt.label
1327
+ [stmt.pre]: #stmt.pre
1328
+ [stmt.ranged]: #stmt.ranged
1329
+ [stmt.return]: #stmt.return
1330
+ [stmt.return.coroutine]: #stmt.return.coroutine
1331
+ [stmt.select]: #stmt.select
1332
+ [stmt.select.general]: #stmt.select.general
1333
+ [stmt.switch]: #stmt.switch
1334
+ [stmt.while]: #stmt.while
1335
+ [support.start.term]: support.md#support.start.term
1336
+ [temp.decls.general]: temp.md#temp.decls.general
1337
+ [temp.pre]: temp.md#temp.pre
1338
+ [term.odr.use]: basic.md#term.odr.use
1339
+ [thread.thread.this]: thread.md#thread.thread.this
1340
+
1341
+ [^1]: In other words, the `else` is associated with the nearest un-elsed
1342
+ `if`.
1343
+
1344
+ [^2]: The transfer from the condition of a `switch` statement to a
1345
+ `case` label is considered a jump in this respect.