- tmp/tmpyl690px2/{from.md → to.md} +289 -262
tmp/tmpyl690px2/{from.md → to.md}
RENAMED
|
@@ -12,104 +12,109 @@ 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 |
init-statement:
|
| 19 |
expression-statement
|
| 20 |
simple-declaration
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
condition:
|
| 23 |
expression
|
| 24 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator brace-or-equal-initializer
|
| 25 |
```
|
| 26 |
|
| 27 |
The optional *attribute-specifier-seq* appertains to the respective
|
| 28 |
statement.
|
| 29 |
|
| 30 |
A *substatement* of a *statement* is one of the following:
|
| 31 |
|
| 32 |
-
- for a *labeled-statement*, its
|
| 33 |
- for a *compound-statement*, any *statement* of its *statement-seq*,
|
| 34 |
-
- for a *selection-statement*, any of its *statement*s
|
| 35 |
-
*init-statement*), or
|
| 36 |
-
- for an *iteration-statement*, its
|
| 37 |
*init-statement*).
|
| 38 |
|
| 39 |
[*Note 1*: The *compound-statement* of a *lambda-expression* is not a
|
| 40 |
substatement of the *statement* (if any) in which the
|
| 41 |
*lambda-expression* lexically appears. — *end note*]
|
| 42 |
|
| 43 |
A *statement* `S1` *encloses* a *statement* `S2` if
|
| 44 |
|
| 45 |
-
- `S2` is a substatement of `S1`
|
| 46 |
- `S1` is a *selection-statement* or *iteration-statement* and `S2` is
|
| 47 |
the *init-statement* of `S1`,
|
| 48 |
- `S1` is a *try-block* and `S2` is its *compound-statement* or any of
|
| 49 |
the *compound-statement*s of its *handler*s, or
|
| 50 |
- `S1` encloses a statement `S3` and `S3` encloses `S2`.
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
not an *expression* is a declaration [[dcl.dcl]]. The *declarator* shall
|
| 55 |
-
not specify a function or an array. The *decl-specifier-seq* shall not
|
| 56 |
-
define a class or enumeration. If the `auto` *type-specifier* appears in
|
| 57 |
-
the *decl-specifier-seq*, the type of the identifier being declared is
|
| 58 |
-
deduced from the initializer as described in [[dcl.spec.auto]].
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
The value of a *condition* that is an initialized declaration in a
|
| 67 |
statement other than a `switch` statement is the value of the declared
|
| 68 |
variable contextually converted to `bool` [[conv]]. If that conversion
|
| 69 |
is ill-formed, the program is ill-formed. The value of a *condition*
|
| 70 |
-
that is an
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
than `switch`; if that conversion is ill-formed, the program is
|
| 76 |
-
ill-formed. The value of the condition will be referred to as simply
|
| 77 |
-
“the condition” where the usage is unambiguous.
|
| 78 |
|
| 79 |
If a *condition* can be syntactically resolved as either an expression
|
| 80 |
-
or
|
| 81 |
-
declaration.
|
| 82 |
|
| 83 |
In the *decl-specifier-seq* of a *condition*, each *decl-specifier*
|
| 84 |
shall be either a *type-specifier* or `constexpr`.
|
| 85 |
|
| 86 |
-
##
|
| 87 |
|
| 88 |
-
A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
``` bnf
|
| 91 |
labeled-statement:
|
| 92 |
-
|
| 93 |
-
attribute-specifier-seqₒₚₜ case constant-expression ':' statement
|
| 94 |
-
attribute-specifier-seqₒₚₜ default ':' statement
|
| 95 |
```
|
| 96 |
|
| 97 |
-
The optional *attribute-specifier-seq* appertains to the label.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
a function. A label can be used in a `goto` statement before its
|
| 102 |
-
declaration. Labels have their own name space and do not interfere with
|
| 103 |
-
other identifiers.
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
Unqualified name lookup [[basic.lookup.unqual]] ignores
|
| 108 |
-
labels. — *end note*]
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
## Expression statement <a id="stmt.expr">[[stmt.expr]]</a>
|
| 113 |
|
| 114 |
Expression statements have the form
|
| 115 |
|
|
@@ -122,108 +127,101 @@ The expression is a discarded-value expression [[expr.context]]. All
|
|
| 122 |
side effects from an expression statement are completed before the next
|
| 123 |
statement is executed. An expression statement with the expression
|
| 124 |
missing is called a *null statement*.
|
| 125 |
|
| 126 |
[*Note 1*: Most statements are expression statements — usually
|
| 127 |
-
assignments or function calls. A null statement is useful to
|
| 128 |
-
|
| 129 |
-
body to an iteration statement such as a `while` statement
|
| 130 |
[[stmt.while]]. — *end note*]
|
| 131 |
|
| 132 |
## Compound statement or block <a id="stmt.block">[[stmt.block]]</a>
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
|
| 137 |
``` bnf
|
| 138 |
compound-statement:
|
| 139 |
-
'{' statement-seqₒₚₜ '}'
|
| 140 |
```
|
| 141 |
|
| 142 |
``` bnf
|
| 143 |
statement-seq:
|
| 144 |
statement
|
| 145 |
statement-seq statement
|
| 146 |
```
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
## Selection statements <a id="stmt.select">[[stmt.select]]</a>
|
| 153 |
|
|
|
|
|
|
|
| 154 |
Selection statements choose one of several flows of control.
|
| 155 |
|
| 156 |
``` bnf
|
| 157 |
selection-statement:
|
| 158 |
if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement
|
| 159 |
if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement else statement
|
|
|
|
|
|
|
| 160 |
switch '(' init-statementₒₚₜ condition ')' statement
|
| 161 |
```
|
| 162 |
|
| 163 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 164 |
condition.
|
| 165 |
|
| 166 |
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
[[basic.scope]].
|
| 171 |
-
single statement and not a *compound-statement*, it is as if it was
|
| 172 |
-
rewritten to be a *compound-statement* containing the original
|
| 173 |
-
substatement.
|
| 174 |
-
|
| 175 |
-
[*Example 1*:
|
| 176 |
-
|
| 177 |
-
``` cpp
|
| 178 |
-
if (x)
|
| 179 |
-
int i;
|
| 180 |
-
```
|
| 181 |
-
|
| 182 |
-
can be equivalently rewritten as
|
| 183 |
-
|
| 184 |
-
``` cpp
|
| 185 |
-
if (x) {
|
| 186 |
-
int i;
|
| 187 |
-
}
|
| 188 |
-
```
|
| 189 |
-
|
| 190 |
-
Thus after the `if` statement, `i` is no longer in scope.
|
| 191 |
-
|
| 192 |
-
— *end example*]
|
| 193 |
|
| 194 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 195 |
|
| 196 |
-
If the condition [[stmt.
|
| 197 |
executed. If the `else` part of the selection statement is present and
|
| 198 |
the condition yields `false`, the second substatement is executed. If
|
| 199 |
the first substatement is reached via a label, the condition is not
|
| 200 |
evaluated and the second substatement is not executed. In the second
|
| 201 |
form of `if` statement (the one including `else`), if the first
|
| 202 |
substatement is also an `if` statement then that inner `if` statement
|
| 203 |
shall contain an `else` part.[^1]
|
| 204 |
|
| 205 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 206 |
-
condition
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
substatement is a *discarded statement*,
|
| 210 |
-
substatement, if present, is a discarded statement.
|
| 211 |
-
instantiation of an enclosing templated entity [[temp.pre]],
|
| 212 |
-
condition is not value-dependent after its instantiation, the
|
| 213 |
-
substatement (if any) is not instantiated.
|
| 214 |
-
|
| 215 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
require an entity to be defined. — *end note*]
|
| 217 |
|
| 218 |
-
|
| 219 |
-
shall be associated with a `switch` statement [[stmt.switch]] within the
|
| 220 |
-
same `if` statement. A label [[stmt.label]] declared in a substatement
|
| 221 |
-
of a constexpr if statement shall only be referred to by a statement
|
| 222 |
-
[[stmt.goto]] in the same substatement.
|
| 223 |
-
|
| 224 |
-
[*Example 1*:
|
| 225 |
|
| 226 |
``` cpp
|
| 227 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 228 |
// ... handle p
|
| 229 |
|
|
@@ -273,25 +271,80 @@ is equivalent to
|
|
| 273 |
init-statement
|
| 274 |
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 275 |
'}'
|
| 276 |
```
|
| 277 |
|
| 278 |
-
except that
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 282 |
|
| 283 |
The `switch` statement causes control to be transferred to one of
|
| 284 |
several statements depending on the value of a condition.
|
| 285 |
|
| 286 |
-
The
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
| 293 |
|
| 294 |
``` bnf
|
| 295 |
case constant-expression ':'
|
| 296 |
```
|
| 297 |
|
|
@@ -342,15 +395,17 @@ is equivalent to
|
|
| 342 |
init-statement
|
| 343 |
switch '(' condition ')' statement
|
| 344 |
'}'
|
| 345 |
```
|
| 346 |
|
| 347 |
-
except that
|
| 348 |
-
|
| 349 |
|
| 350 |
## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
|
| 351 |
|
|
|
|
|
|
|
| 352 |
Iteration statements specify looping.
|
| 353 |
|
| 354 |
``` bnf
|
| 355 |
iteration-statement:
|
| 356 |
while '(' condition ')' statement
|
|
@@ -398,37 +453,17 @@ while (--x >= 0) {
|
|
| 398 |
|
| 399 |
Thus after the `while` statement, `i` is no longer in scope.
|
| 400 |
|
| 401 |
— *end example*]
|
| 402 |
|
| 403 |
-
If a name introduced in an *init-statement* or *for-range-declaration*
|
| 404 |
-
is redeclared in the outermost block of the substatement, the program is
|
| 405 |
-
ill-formed.
|
| 406 |
-
|
| 407 |
-
[*Example 2*:
|
| 408 |
-
|
| 409 |
-
``` cpp
|
| 410 |
-
void f() {
|
| 411 |
-
for (int i = 0; i < 10; ++i)
|
| 412 |
-
int i = 0; // error: redeclaration
|
| 413 |
-
for (int i : { 1, 2, 3 })
|
| 414 |
-
int i = 1; // error: redeclaration
|
| 415 |
-
}
|
| 416 |
-
```
|
| 417 |
-
|
| 418 |
-
— *end example*]
|
| 419 |
-
|
| 420 |
### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
|
| 421 |
|
| 422 |
In the `while` statement the substatement is executed repeatedly until
|
| 423 |
-
the value of the condition [[stmt.
|
| 424 |
-
|
| 425 |
|
| 426 |
-
|
| 427 |
-
the variable that is declared extends from its point of declaration
|
| 428 |
-
[[basic.scope.pdecl]] to the end of the `while` *statement*. A `while`
|
| 429 |
-
statement is equivalent to
|
| 430 |
|
| 431 |
``` bnf
|
| 432 |
label ':'
|
| 433 |
'{'
|
| 434 |
if '(' condition ')' '{'
|
|
@@ -494,42 +529,25 @@ is equivalent to
|
|
| 494 |
expression ';'
|
| 495 |
'}'
|
| 496 |
'}'
|
| 497 |
```
|
| 498 |
|
| 499 |
-
except that
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
|
| 504 |
[*Note 1*: Thus the first statement specifies initialization for the
|
| 505 |
-
loop; the condition [[stmt.
|
| 506 |
-
|
| 507 |
`false`; the expression often specifies incrementing that is sequenced
|
| 508 |
after each iteration. — *end note*]
|
| 509 |
|
| 510 |
Either or both of the *condition* and the *expression* can be omitted. A
|
| 511 |
missing *condition* makes the implied `while` clause equivalent to
|
| 512 |
`while(true)`.
|
| 513 |
|
| 514 |
-
If the *init-statement* is a declaration, the scope of the name(s)
|
| 515 |
-
declared extends to the end of the `for` statement.
|
| 516 |
-
|
| 517 |
-
[*Example 1*:
|
| 518 |
-
|
| 519 |
-
``` cpp
|
| 520 |
-
int i = 42;
|
| 521 |
-
int a[10];
|
| 522 |
-
|
| 523 |
-
for (int i = 0; i < 10; i++)
|
| 524 |
-
a[i] = i;
|
| 525 |
-
|
| 526 |
-
int j = i; // j = 42
|
| 527 |
-
```
|
| 528 |
-
|
| 529 |
-
— *end example*]
|
| 530 |
-
|
| 531 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 532 |
|
| 533 |
The range-based `for` statement
|
| 534 |
|
| 535 |
``` bnf
|
|
@@ -556,25 +574,24 @@ where
|
|
| 556 |
- if the *for-range-initializer* is an *expression*, it is regarded as
|
| 557 |
if it were surrounded by parentheses (so that a comma operator cannot
|
| 558 |
be reinterpreted as delimiting two *init-declarator*s);
|
| 559 |
- *`range`*, *`begin`*, and *`end`* are variables defined for exposition
|
| 560 |
only; and
|
| 561 |
-
- *begin-expr* and *end-expr* are determined as follows:
|
| 562 |
- if the *for-range-initializer* is an expression of array type `R`,
|
| 563 |
-
*begin-expr* and *end-expr* are *`range`* and *`range`* `+` `N`,
|
| 564 |
respectively, where `N` is the array bound. If `R` is an array of
|
| 565 |
unknown bound or an array of incomplete type, the program is
|
| 566 |
ill-formed;
|
| 567 |
- if the *for-range-initializer* is an expression of class type `C`,
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
declaration, *begin-expr* and *end-expr* are `range.begin()` and
|
| 572 |
`range.end()`, respectively;
|
| 573 |
-
- otherwise, *begin-expr* and *end-expr* are `begin(range)` and
|
| 574 |
-
`end(range)`, respectively, where `begin` and `end`
|
| 575 |
-
|
| 576 |
\[*Note 1*: Ordinary unqualified lookup [[basic.lookup.unqual]] is
|
| 577 |
not performed. — *end note*]
|
| 578 |
|
| 579 |
[*Example 1*:
|
| 580 |
|
|
@@ -584,16 +601,38 @@ for (int& x : array)
|
|
| 584 |
x *= 2;
|
| 585 |
```
|
| 586 |
|
| 587 |
— *end example*]
|
| 588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
In the *decl-specifier-seq* of a *for-range-declaration*, each
|
| 590 |
*decl-specifier* shall be either a *type-specifier* or `constexpr`. The
|
| 591 |
*decl-specifier-seq* shall not define a class or enumeration.
|
| 592 |
|
| 593 |
## Jump statements <a id="stmt.jump">[[stmt.jump]]</a>
|
| 594 |
|
|
|
|
|
|
|
| 595 |
Jump statements unconditionally transfer control.
|
| 596 |
|
| 597 |
``` bnf
|
| 598 |
jump-statement:
|
| 599 |
break ';'
|
|
@@ -601,43 +640,36 @@ jump-statement:
|
|
| 601 |
return expr-or-braced-init-listₒₚₜ ';'
|
| 602 |
coroutine-return-statement
|
| 603 |
goto identifier ';'
|
| 604 |
```
|
| 605 |
|
| 606 |
-
On exit from a scope (however accomplished), objects with
|
| 607 |
-
storage duration [[basic.stc.auto]] that have been constructed
|
| 608 |
-
scope are destroyed in the reverse order of their construction.
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
transferred from but not at the point transferred to. (See [[stmt.dcl]]
|
| 616 |
-
for transfers into blocks).
|
| 617 |
-
|
| 618 |
-
[*Note 2*: However, the program can be terminated (by calling
|
| 619 |
-
`std::exit()` or `std::abort()` [[support.start.term]], for example)
|
| 620 |
-
without destroying objects with automatic storage
|
| 621 |
-
duration. — *end note*]
|
| 622 |
-
|
| 623 |
-
[*Note 3*: A suspension of a coroutine [[expr.await]] is not considered
|
| 624 |
to be an exit from a scope. — *end note*]
|
| 625 |
|
| 626 |
### The `break` statement <a id="stmt.break">[[stmt.break]]</a>
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
|
|
|
| 632 |
|
| 633 |
### The `continue` statement <a id="stmt.cont">[[stmt.cont]]</a>
|
| 634 |
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
|
|
|
| 639 |
|
| 640 |
``` cpp
|
| 641 |
while (foo) {
|
| 642 |
{
|
| 643 |
// ...
|
|
@@ -673,39 +705,32 @@ A function returns to its caller by the `return` statement.
|
|
| 673 |
|
| 674 |
The *expr-or-braced-init-list* of a `return` statement is called its
|
| 675 |
operand. A `return` statement with no operand shall be used only in a
|
| 676 |
function whose return type is cv `void`, a constructor [[class.ctor]],
|
| 677 |
or a destructor [[class.dtor]]. A `return` statement with an operand of
|
| 678 |
-
type `void` shall be used only in a function
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
statement initializes the
|
| 682 |
-
(explicit or implicit) function call by copy-initialization
|
| 683 |
-
from the operand.
|
| 684 |
|
| 685 |
-
[*Note 1*: A
|
|
|
|
|
|
|
|
|
|
| 686 |
constructor to perform a copy or move of the operand if it is not a
|
| 687 |
prvalue or if its type differs from the return type of the function. A
|
| 688 |
-
copy operation associated with a `return` statement
|
| 689 |
converted to a move operation if an automatic storage duration variable
|
| 690 |
is returned [[class.copy.elision]]. — *end note*]
|
| 691 |
|
|
|
|
|
|
|
|
|
|
| 692 |
[*Example 1*:
|
| 693 |
|
| 694 |
-
``` cpp
|
| 695 |
-
std::pair<std::string,int> f(const char* p, int x) {
|
| 696 |
-
return {p,x};
|
| 697 |
-
}
|
| 698 |
-
```
|
| 699 |
-
|
| 700 |
-
— *end example*]
|
| 701 |
-
|
| 702 |
-
The destructor for the returned object is potentially invoked (
|
| 703 |
-
[[class.dtor]], [[except.ctor]]).
|
| 704 |
-
|
| 705 |
-
[*Example 2*:
|
| 706 |
-
|
| 707 |
``` cpp
|
| 708 |
class A {
|
| 709 |
~A() {}
|
| 710 |
};
|
| 711 |
A f() { return A(); } // error: destructor of A is private (even though it is never invoked)
|
|
@@ -713,12 +738,12 @@ A f() { return A(); } // error: destructor of A is private (even though it is
|
|
| 713 |
|
| 714 |
— *end example*]
|
| 715 |
|
| 716 |
Flowing off the end of a constructor, a destructor, or a non-coroutine
|
| 717 |
function with a cv `void` return type is equivalent to a `return` with
|
| 718 |
-
no operand. Otherwise, flowing off the end of a function
|
| 719 |
-
`main` [[basic.start.main]]
|
| 720 |
results in undefined behavior.
|
| 721 |
|
| 722 |
The copy-initialization of the result of the call is sequenced before
|
| 723 |
the destruction of temporaries at the end of the full-expression
|
| 724 |
established by the operand of the `return` statement, which, in turn, is
|
|
@@ -757,46 +782,49 @@ where *`final-suspend`* is the exposition-only label defined in
|
|
| 757 |
- Otherwise, *S* is the *compound-statement* `{` *expression*ₒₚₜ `;`
|
| 758 |
*p*`.return_void()``; }`. The expression *p*`.return_void()` shall be
|
| 759 |
a prvalue of type `void`.
|
| 760 |
|
| 761 |
If *p*`.return_void()` is a valid expression, flowing off the end of a
|
| 762 |
-
coroutine is equivalent to a `co_return` with no
|
| 763 |
-
flowing off the end of a coroutine
|
|
|
|
| 764 |
|
| 765 |
### The `goto` statement <a id="stmt.goto">[[stmt.goto]]</a>
|
| 766 |
|
| 767 |
The `goto` statement unconditionally transfers control to the statement
|
| 768 |
labeled by the identifier. The identifier shall be a label
|
| 769 |
[[stmt.label]] located in the current function.
|
| 770 |
|
| 771 |
## Declaration statement <a id="stmt.dcl">[[stmt.dcl]]</a>
|
| 772 |
|
| 773 |
-
A declaration statement introduces one or more new
|
| 774 |
-
|
| 775 |
|
| 776 |
``` bnf
|
| 777 |
declaration-statement:
|
| 778 |
block-declaration
|
| 779 |
```
|
| 780 |
|
| 781 |
-
If an identifier introduced by a declaration was previously
|
| 782 |
-
an outer block, the outer declaration is hidden for the
|
| 783 |
-
block, after which it resumes
|
|
|
|
| 784 |
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
variable with automatic storage duration is not in scope to a point
|
| 794 |
-
where it is in scope is ill-formed unless the variable has vacuous
|
| 795 |
-
initialization [[basic.life]]. In such a case, the variables with
|
| 796 |
-
vacuous initialization are constructed in the order of their
|
| 797 |
-
declaration.
|
| 798 |
|
| 799 |
[*Example 1*:
|
| 800 |
|
| 801 |
``` cpp
|
| 802 |
void f() {
|
|
@@ -812,21 +840,27 @@ lx:
|
|
| 812 |
}
|
| 813 |
```
|
| 814 |
|
| 815 |
— *end example*]
|
| 816 |
|
| 817 |
-
Dynamic initialization of a block
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 828 |
|
| 829 |
[*Example 2*:
|
| 830 |
|
| 831 |
``` cpp
|
| 832 |
int foo(int i) {
|
|
@@ -835,16 +869,15 @@ int foo(int i) {
|
|
| 835 |
}
|
| 836 |
```
|
| 837 |
|
| 838 |
— *end example*]
|
| 839 |
|
| 840 |
-
|
| 841 |
-
destroyed if and only if it was constructed.
|
| 842 |
|
| 843 |
-
[*Note
|
| 844 |
-
|
| 845 |
-
destroyed. — *end note*]
|
| 846 |
|
| 847 |
## Ambiguity resolution <a id="stmt.ambig">[[stmt.ambig]]</a>
|
| 848 |
|
| 849 |
There is an ambiguity in the grammar involving *expression-statement*s
|
| 850 |
and *declaration*s: An *expression-statement* with a function-style
|
|
@@ -854,13 +887,13 @@ first *declarator* starts with a `(`. In those cases the *statement* is
|
|
| 854 |
a *declaration*.
|
| 855 |
|
| 856 |
[*Note 1*:
|
| 857 |
|
| 858 |
If the *statement* cannot syntactically be a *declaration*, there is no
|
| 859 |
-
ambiguity, so this rule does not apply.
|
| 860 |
-
to be examined to determine whether this is the case.
|
| 861 |
-
meaning of many examples.
|
| 862 |
|
| 863 |
[*Example 1*:
|
| 864 |
|
| 865 |
Assuming `T` is a *simple-type-specifier* [[dcl.type]],
|
| 866 |
|
|
@@ -909,16 +942,13 @@ The disambiguation is purely syntactic; that is, the meaning of the
|
|
| 909 |
names occurring in such a statement, beyond whether they are
|
| 910 |
*type-name*s or not, is not generally used in or changed by the
|
| 911 |
disambiguation. Class templates are instantiated as necessary to
|
| 912 |
determine if a qualified name is a *type-name*. Disambiguation precedes
|
| 913 |
parsing, and a statement disambiguated as a declaration may be an
|
| 914 |
-
ill-formed declaration. If, during parsing,
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
[*Note 2*: This can occur only when the name is declared earlier in the
|
| 919 |
-
declaration. — *end note*]
|
| 920 |
|
| 921 |
[*Example 3*:
|
| 922 |
|
| 923 |
``` cpp
|
| 924 |
struct T1 {
|
|
@@ -939,26 +969,24 @@ void f() {
|
|
| 939 |
```
|
| 940 |
|
| 941 |
— *end example*]
|
| 942 |
|
| 943 |
<!-- Link reference definitions -->
|
| 944 |
-
[basic.def.odr]: basic.md#basic.def.odr
|
| 945 |
[basic.life]: basic.md#basic.life
|
| 946 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 947 |
-
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 948 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 949 |
[basic.scope]: basic.md#basic.scope
|
| 950 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 951 |
-
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 952 |
[basic.start.main]: basic.md#basic.start.main
|
| 953 |
[basic.start.term]: basic.md#basic.start.term
|
| 954 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 955 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 956 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 957 |
[class.copy.elision]: class.md#class.copy.elision
|
| 958 |
[class.ctor]: class.md#class.ctor
|
| 959 |
[class.dtor]: class.md#class.dtor
|
|
|
|
| 960 |
[class.temporary]: basic.md#class.temporary
|
| 961 |
[conv]: expr.md#conv
|
| 962 |
[conv.prom]: expr.md#conv.prom
|
| 963 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 964 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
|
@@ -980,28 +1008,27 @@ void f() {
|
|
| 980 |
[stmt.expr]: #stmt.expr
|
| 981 |
[stmt.for]: #stmt.for
|
| 982 |
[stmt.goto]: #stmt.goto
|
| 983 |
[stmt.if]: #stmt.if
|
| 984 |
[stmt.iter]: #stmt.iter
|
|
|
|
| 985 |
[stmt.jump]: #stmt.jump
|
|
|
|
| 986 |
[stmt.label]: #stmt.label
|
| 987 |
[stmt.pre]: #stmt.pre
|
| 988 |
[stmt.ranged]: #stmt.ranged
|
| 989 |
[stmt.return]: #stmt.return
|
| 990 |
[stmt.return.coroutine]: #stmt.return.coroutine
|
| 991 |
[stmt.select]: #stmt.select
|
|
|
|
| 992 |
[stmt.stmt]: #stmt.stmt
|
| 993 |
[stmt.switch]: #stmt.switch
|
| 994 |
[stmt.while]: #stmt.while
|
| 995 |
[support.start.term]: support.md#support.start.term
|
| 996 |
[temp.pre]: temp.md#temp.pre
|
|
|
|
| 997 |
|
| 998 |
[^1]: In other words, the `else` is associated with the nearest un-elsed
|
| 999 |
`if`.
|
| 1000 |
|
| 1001 |
[^2]: The transfer from the condition of a `switch` statement to a
|
| 1002 |
`case` label is considered a jump in this respect.
|
| 1003 |
-
|
| 1004 |
-
[^3]: The implementation must not introduce any deadlock around
|
| 1005 |
-
execution of the initializer. Deadlocks might still be caused by the
|
| 1006 |
-
program logic; the implementation need only avoid deadlocks due to
|
| 1007 |
-
its own synchronization operations.
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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 ')' '{'
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 ';'
|
|
|
|
| 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 |
// ...
|
|
|
|
| 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)
|
|
|
|
| 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
|
|
|
|
| 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() {
|
|
|
|
| 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) {
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 {
|
|
|
|
| 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
|
|
|
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|