From Jason Turner

[stmt.stmt]

Diff to HTML by rtfpessoa

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