- tmp/tmp8l55vvjw/{from.md → to.md} +308 -198
tmp/tmp8l55vvjw/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
# Statements <a id="stmt.stmt">[[stmt.stmt]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
Except as indicated, statements are executed in sequence.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
statement:
|
| 7 |
labeled-statement
|
|
@@ -23,50 +25,58 @@ condition:
|
|
| 23 |
```
|
| 24 |
|
| 25 |
The optional *attribute-specifier-seq* appertains to the respective
|
| 26 |
statement.
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
The rules for *condition*s apply both to *selection-statement*s and to
|
| 29 |
-
the `for` and `while` statements
|
| 30 |
-
|
| 31 |
-
not
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
[[dcl.spec.auto]].
|
| 35 |
-
|
| 36 |
-
A name introduced
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
block
|
| 41 |
-
that redeclares the name is ill-formed.
|
| 42 |
-
|
| 43 |
-
[*Example 1*:
|
| 44 |
-
|
| 45 |
-
``` cpp
|
| 46 |
-
if (int x = f()) {
|
| 47 |
-
int x; // ill-formed, redeclaration of x
|
| 48 |
-
}
|
| 49 |
-
else {
|
| 50 |
-
int x; // ill-formed, redeclaration of x
|
| 51 |
-
}
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
— *end example*]
|
| 55 |
|
| 56 |
The value of a *condition* that is an initialized declaration in a
|
| 57 |
statement other than a `switch` statement is the value of the declared
|
| 58 |
-
variable contextually converted to `bool`
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
ill-formed
|
| 67 |
-
|
| 68 |
|
| 69 |
If a *condition* can be syntactically resolved as either an expression
|
| 70 |
or the declaration of a block-scope name, it is interpreted as a
|
| 71 |
declaration.
|
| 72 |
|
|
@@ -78,12 +88,12 @@ shall be either a *type-specifier* or `constexpr`.
|
|
| 78 |
A statement can be labeled.
|
| 79 |
|
| 80 |
``` bnf
|
| 81 |
labeled-statement:
|
| 82 |
attribute-specifier-seqₒₚₜ identifier ':' statement
|
| 83 |
-
attribute-specifier-seqₒₚₜ
|
| 84 |
-
attribute-specifier-seqₒₚₜ
|
| 85 |
```
|
| 86 |
|
| 87 |
The optional *attribute-specifier-seq* appertains to the label. An
|
| 88 |
*identifier label* declares the identifier. The only use of an
|
| 89 |
identifier label is as the target of a `goto`. The scope of a label is
|
|
@@ -92,34 +102,34 @@ a function. A label can be used in a `goto` statement before its
|
|
| 92 |
declaration. Labels have their own name space and do not interfere with
|
| 93 |
other identifiers.
|
| 94 |
|
| 95 |
[*Note 1*: A label may have the same name as another declaration in the
|
| 96 |
same scope or a *template-parameter* from an enclosing scope.
|
| 97 |
-
Unqualified name lookup
|
| 98 |
labels. — *end note*]
|
| 99 |
|
| 100 |
-
Case labels and default labels shall occur only in switch statements.
|
| 101 |
|
| 102 |
## Expression statement <a id="stmt.expr">[[stmt.expr]]</a>
|
| 103 |
|
| 104 |
Expression statements have the form
|
| 105 |
|
| 106 |
``` bnf
|
| 107 |
expression-statement:
|
| 108 |
expressionₒₚₜ ';'
|
| 109 |
```
|
| 110 |
|
| 111 |
-
The expression is a discarded-value expression
|
| 112 |
side effects from an expression statement are completed before the next
|
| 113 |
statement is executed. An expression statement with the expression
|
| 114 |
missing is called a *null statement*.
|
| 115 |
|
| 116 |
[*Note 1*: Most statements are expression statements — usually
|
| 117 |
assignments or function calls. A null statement is useful to carry a
|
| 118 |
label just before the `}` of a compound statement and to supply a null
|
| 119 |
-
body to an iteration statement such as a `while` statement
|
| 120 |
-
[[stmt.while]]
|
| 121 |
|
| 122 |
## Compound statement or block <a id="stmt.block">[[stmt.block]]</a>
|
| 123 |
|
| 124 |
So that several statements can be used where one is expected, the
|
| 125 |
compound statement (also, and equivalently, called “block”) is provided.
|
|
@@ -133,39 +143,36 @@ compound-statement:
|
|
| 133 |
statement-seq:
|
| 134 |
statement
|
| 135 |
statement-seq statement
|
| 136 |
```
|
| 137 |
|
| 138 |
-
A compound statement defines a block scope
|
| 139 |
|
| 140 |
-
[*Note 1*: A declaration is a *statement*
|
| 141 |
-
[[stmt.dcl]]). — *end note*]
|
| 142 |
|
| 143 |
## Selection statements <a id="stmt.select">[[stmt.select]]</a>
|
| 144 |
|
| 145 |
Selection statements choose one of several flows of control.
|
| 146 |
|
| 147 |
``` bnf
|
| 148 |
selection-statement:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
```
|
| 153 |
|
| 154 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 155 |
condition.
|
| 156 |
|
| 157 |
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
*compound-statement*, it is as if it was rewritten to be a
|
| 166 |
-
compound-statement containing the original substatement.
|
| 167 |
|
| 168 |
[*Example 1*:
|
| 169 |
|
| 170 |
``` cpp
|
| 171 |
if (x)
|
|
@@ -184,37 +191,37 @@ Thus after the `if` statement, `i` is no longer in scope.
|
|
| 184 |
|
| 185 |
— *end example*]
|
| 186 |
|
| 187 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 188 |
|
| 189 |
-
If the condition
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
evaluated and the second substatement is not executed. In the second
|
| 194 |
form of `if` statement (the one including `else`), if the first
|
| 195 |
substatement is also an `if` statement then that inner `if` statement
|
| 196 |
shall contain an `else` part.[^1]
|
| 197 |
|
| 198 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 199 |
condition shall be a contextually converted constant expression of type
|
| 200 |
-
`bool`
|
| 201 |
-
|
| 202 |
substatement is a *discarded statement*, otherwise the second
|
| 203 |
substatement, if present, is a discarded statement. During the
|
| 204 |
-
|
| 205 |
condition is not value-dependent after its instantiation, the discarded
|
| 206 |
substatement (if any) is not instantiated.
|
| 207 |
|
| 208 |
-
[*Note 1*: Odr-uses
|
| 209 |
-
|
| 210 |
|
| 211 |
A `case` or `default` label appearing within such an `if` statement
|
| 212 |
-
shall be associated with a `switch` statement
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
[*Example 1*:
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
|
@@ -239,47 +246,61 @@ int f() {
|
|
| 239 |
— *end example*]
|
| 240 |
|
| 241 |
An `if` statement of the form
|
| 242 |
|
| 243 |
``` bnf
|
| 244 |
-
|
| 245 |
```
|
| 246 |
|
| 247 |
is equivalent to
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
and an `if` statement of the form
|
| 250 |
|
| 251 |
``` bnf
|
| 252 |
-
|
| 253 |
```
|
| 254 |
|
| 255 |
is equivalent to
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
except that names declared in the *init-statement* are in the same
|
| 258 |
declarative region as those declared in the *condition*.
|
| 259 |
|
| 260 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 261 |
|
| 262 |
The `switch` statement causes control to be transferred to one of
|
| 263 |
several statements depending on the value of a condition.
|
| 264 |
|
| 265 |
The condition shall be of integral type, enumeration type, or class
|
| 266 |
type. If of class type, the condition is contextually implicitly
|
| 267 |
-
converted
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
|
| 273 |
``` bnf
|
| 274 |
-
|
| 275 |
```
|
| 276 |
|
| 277 |
-
where the *constant-expression* shall be a converted constant
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
|
| 282 |
There shall be at most one label of the form
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
default :
|
|
@@ -288,49 +309,56 @@ default :
|
|
| 288 |
within a `switch` statement.
|
| 289 |
|
| 290 |
Switch statements can be nested; a `case` or `default` label is
|
| 291 |
associated with the smallest switch enclosing it.
|
| 292 |
|
| 293 |
-
When the `switch` statement is executed, its condition is evaluated
|
| 294 |
-
|
| 295 |
-
to the
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
`case` and `default` labels in themselves do not alter the flow of
|
| 302 |
control, which continues unimpeded across such labels. To exit from a
|
| 303 |
-
switch, see `break`,
|
| 304 |
|
| 305 |
[*Note 1*: Usually, the substatement that is the subject of a switch is
|
| 306 |
compound and `case` and `default` labels appear on the top-level
|
| 307 |
statements contained within the (compound) substatement, but this is not
|
| 308 |
required. Declarations can appear in the substatement of a `switch`
|
| 309 |
statement. — *end note*]
|
| 310 |
|
| 311 |
A `switch` statement of the form
|
| 312 |
|
| 313 |
``` bnf
|
| 314 |
-
|
| 315 |
```
|
| 316 |
|
| 317 |
is equivalent to
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
except that names declared in the *init-statement* are in the same
|
| 320 |
declarative region as those declared in the *condition*.
|
| 321 |
|
| 322 |
## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
|
| 323 |
|
| 324 |
Iteration statements specify looping.
|
| 325 |
|
| 326 |
``` bnf
|
| 327 |
iteration-statement:
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
```
|
| 333 |
|
| 334 |
``` bnf
|
| 335 |
for-range-declaration:
|
| 336 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
|
@@ -346,16 +374,14 @@ See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
|
| 346 |
*for-range-declaration*.
|
| 347 |
|
| 348 |
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 349 |
|
| 350 |
The substatement in an *iteration-statement* implicitly defines a block
|
| 351 |
-
scope
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
not a *compound-statement*, it is as if it was rewritten to be a
|
| 356 |
-
compound-statement containing the original statement.
|
| 357 |
|
| 358 |
[*Example 1*:
|
| 359 |
|
| 360 |
``` cpp
|
| 361 |
while (--x >= 0)
|
|
@@ -392,36 +418,31 @@ void f() {
|
|
| 392 |
— *end example*]
|
| 393 |
|
| 394 |
### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
|
| 395 |
|
| 396 |
In the `while` statement the substatement is executed repeatedly until
|
| 397 |
-
the value of the condition
|
| 398 |
takes place before each execution of the substatement.
|
| 399 |
|
| 400 |
When the condition of a `while` statement is a declaration, the scope of
|
| 401 |
-
the variable that is declared extends from its point of declaration
|
| 402 |
-
[[basic.scope.pdecl]]
|
| 403 |
-
statement
|
| 404 |
|
| 405 |
-
```
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
is equivalent to
|
| 410 |
-
|
| 411 |
-
``` cpp
|
| 412 |
-
label:
|
| 413 |
-
{ // start of condition scope
|
| 414 |
-
T t = x;
|
| 415 |
-
if (t) {
|
| 416 |
statement
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
}
|
| 420 |
```
|
| 421 |
|
| 422 |
-
|
|
|
|
|
|
|
| 423 |
iteration of the loop.
|
| 424 |
|
| 425 |
[*Example 1*:
|
| 426 |
|
| 427 |
``` cpp
|
|
@@ -442,39 +463,51 @@ In the while-loop, the constructor and destructor are each called twice,
|
|
| 442 |
once for the condition that succeeds and once for the condition that
|
| 443 |
fails.
|
| 444 |
|
| 445 |
— *end example*]
|
| 446 |
|
|
|
|
|
|
|
| 447 |
### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
|
| 448 |
|
| 449 |
-
The expression is contextually converted to `bool`
|
| 450 |
-
|
| 451 |
|
| 452 |
In the `do` statement the substatement is executed repeatedly until the
|
| 453 |
value of the expression becomes `false`. The test takes place after each
|
| 454 |
execution of the statement.
|
| 455 |
|
| 456 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 457 |
|
| 458 |
The `for` statement
|
| 459 |
|
| 460 |
``` bnf
|
| 461 |
-
|
| 462 |
```
|
| 463 |
|
| 464 |
is equivalent to
|
| 465 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
except that names declared in the *init-statement* are in the same
|
| 467 |
declarative region as those declared in the *condition*, and except that
|
| 468 |
a `continue` in *statement* (not enclosed in another iteration
|
| 469 |
statement) will execute *expression* before re-evaluating *condition*.
|
| 470 |
|
| 471 |
[*Note 1*: Thus the first statement specifies initialization for the
|
| 472 |
-
loop; the condition
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
|
| 477 |
Either or both of the *condition* and the *expression* can be omitted. A
|
| 478 |
missing *condition* makes the implied `while` clause equivalent to
|
| 479 |
`while(true)`.
|
| 480 |
|
|
@@ -498,39 +531,52 @@ int j = i; // j = 42
|
|
| 498 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 499 |
|
| 500 |
The range-based `for` statement
|
| 501 |
|
| 502 |
``` bnf
|
| 503 |
-
|
| 504 |
```
|
| 505 |
|
| 506 |
is equivalent to
|
| 507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
where
|
| 509 |
|
| 510 |
- if the *for-range-initializer* is an *expression*, it is regarded as
|
| 511 |
if it were surrounded by parentheses (so that a comma operator cannot
|
| 512 |
be reinterpreted as delimiting two *init-declarator*s);
|
| 513 |
-
- `
|
| 514 |
only; and
|
| 515 |
- *begin-expr* and *end-expr* are determined as follows:
|
| 516 |
- if the *for-range-initializer* is an expression of array type `R`,
|
| 517 |
-
*begin-expr* and *end-expr* are `
|
| 518 |
-
respectively, where `
|
| 519 |
-
|
| 520 |
ill-formed;
|
| 521 |
- if the *for-range-initializer* is an expression of class type `C`,
|
| 522 |
the *unqualified-id*s `begin` and `end` are looked up in the scope
|
| 523 |
-
of `C` as if by class member access lookup
|
| 524 |
-
[[basic.lookup.classref]]
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
- otherwise, *begin-expr* and *end-expr* are `begin(
|
| 528 |
-
`end(
|
| 529 |
-
|
| 530 |
-
\[*Note 1*: Ordinary unqualified lookup
|
| 531 |
-
|
| 532 |
|
| 533 |
[*Example 1*:
|
| 534 |
|
| 535 |
``` cpp
|
| 536 |
int array[5] = { 1, 2, 3, 4, 5 };
|
|
@@ -548,33 +594,37 @@ In the *decl-specifier-seq* of a *for-range-declaration*, each
|
|
| 548 |
|
| 549 |
Jump statements unconditionally transfer control.
|
| 550 |
|
| 551 |
``` bnf
|
| 552 |
jump-statement:
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
|
|
|
| 557 |
```
|
| 558 |
|
| 559 |
On exit from a scope (however accomplished), objects with automatic
|
| 560 |
-
storage duration
|
| 561 |
-
|
| 562 |
|
| 563 |
[*Note 1*: For temporaries, see [[class.temporary]]. — *end note*]
|
| 564 |
|
| 565 |
Transfer out of a loop, out of a block, or back past an initialized
|
| 566 |
variable with automatic storage duration involves the destruction of
|
| 567 |
objects with automatic storage duration that are in scope at the point
|
| 568 |
transferred from but not at the point transferred to. (See [[stmt.dcl]]
|
| 569 |
for transfers into blocks).
|
| 570 |
|
| 571 |
[*Note 2*: However, the program can be terminated (by calling
|
| 572 |
-
`std::exit()` or `std::abort()`
|
| 573 |
-
without destroying
|
| 574 |
duration. — *end note*]
|
| 575 |
|
|
|
|
|
|
|
|
|
|
| 576 |
### The `break` statement <a id="stmt.break">[[stmt.break]]</a>
|
| 577 |
|
| 578 |
The `break` statement shall occur only in an *iteration-statement* or a
|
| 579 |
`switch` statement and causes termination of the smallest enclosing
|
| 580 |
*iteration-statement* or `switch` statement; control passes to the
|
|
@@ -613,33 +663,33 @@ for (;;) {
|
|
| 613 |
contin: ;
|
| 614 |
}
|
| 615 |
```
|
| 616 |
|
| 617 |
a `continue` not contained in an enclosed iteration statement is
|
| 618 |
-
equivalent to `goto` `contin`.
|
| 619 |
|
| 620 |
### The `return` statement <a id="stmt.return">[[stmt.return]]</a>
|
| 621 |
|
| 622 |
A function returns to its caller by the `return` statement.
|
| 623 |
|
| 624 |
-
The *expr-or-braced-init-list* of a return statement is called its
|
| 625 |
-
operand. A return statement with no operand shall be used only in a
|
| 626 |
-
function whose return type is cv `void`, a constructor
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
|
| 635 |
-
[*Note 1*: A return statement can involve an invocation of a
|
| 636 |
constructor to perform a copy or move of the operand if it is not a
|
| 637 |
prvalue or if its type differs from the return type of the function. A
|
| 638 |
-
copy operation associated with a return statement may be elided or
|
| 639 |
converted to a move operation if an automatic storage duration variable
|
| 640 |
-
is returned
|
| 641 |
|
| 642 |
[*Example 1*:
|
| 643 |
|
| 644 |
``` cpp
|
| 645 |
std::pair<std::string,int> f(const char* p, int x) {
|
|
@@ -647,26 +697,78 @@ std::pair<std::string,int> f(const char* p, int x) {
|
|
| 647 |
}
|
| 648 |
```
|
| 649 |
|
| 650 |
— *end example*]
|
| 651 |
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
| 657 |
The copy-initialization of the result of the call is sequenced before
|
| 658 |
the destruction of temporaries at the end of the full-expression
|
| 659 |
-
established by the operand of the return statement, which, in turn, is
|
| 660 |
-
sequenced before the destruction of local variables
|
| 661 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
|
| 663 |
### The `goto` statement <a id="stmt.goto">[[stmt.goto]]</a>
|
| 664 |
|
| 665 |
The `goto` statement unconditionally transfers control to the statement
|
| 666 |
-
labeled by the identifier. The identifier shall be a label
|
| 667 |
-
[[stmt.label]]
|
| 668 |
|
| 669 |
## Declaration statement <a id="stmt.dcl">[[stmt.dcl]]</a>
|
| 670 |
|
| 671 |
A declaration statement introduces one or more new identifiers into a
|
| 672 |
block; it has the form
|
|
@@ -678,30 +780,30 @@ declaration-statement:
|
|
| 678 |
|
| 679 |
If an identifier introduced by a declaration was previously declared in
|
| 680 |
an outer block, the outer declaration is hidden for the remainder of the
|
| 681 |
block, after which it resumes its force.
|
| 682 |
|
| 683 |
-
Variables with automatic storage duration
|
| 684 |
initialized each time their *declaration-statement* is executed.
|
| 685 |
Variables with automatic storage duration declared in the block are
|
| 686 |
-
destroyed on exit from the block
|
| 687 |
|
| 688 |
It is possible to transfer into a block, but not in a way that bypasses
|
| 689 |
-
declarations with initialization
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
|
| 697 |
[*Example 1*:
|
| 698 |
|
| 699 |
``` cpp
|
| 700 |
void f() {
|
| 701 |
// ...
|
| 702 |
-
goto lx; //
|
| 703 |
// ...
|
| 704 |
ly:
|
| 705 |
X a = 1;
|
| 706 |
// ...
|
| 707 |
lx:
|
|
@@ -711,12 +813,12 @@ lx:
|
|
| 711 |
```
|
| 712 |
|
| 713 |
— *end example*]
|
| 714 |
|
| 715 |
Dynamic initialization of a block-scope variable with static storage
|
| 716 |
-
duration
|
| 717 |
-
[[basic.stc.thread]]
|
| 718 |
its declaration; such a variable is considered initialized upon the
|
| 719 |
completion of its initialization. If the initialization exits by
|
| 720 |
throwing an exception, the initialization is not complete, so it will be
|
| 721 |
tried again the next time control enters the declaration. If control
|
| 722 |
enters the declaration concurrently while the variable is being
|
|
@@ -726,29 +828,29 @@ while the variable is being initialized, the behavior is undefined.
|
|
| 726 |
|
| 727 |
[*Example 2*:
|
| 728 |
|
| 729 |
``` cpp
|
| 730 |
int foo(int i) {
|
| 731 |
-
static int s = foo(2*i); //
|
| 732 |
return i+1;
|
| 733 |
}
|
| 734 |
```
|
| 735 |
|
| 736 |
— *end example*]
|
| 737 |
|
| 738 |
-
|
| 739 |
-
|
| 740 |
|
| 741 |
[*Note 1*: [[basic.start.term]] describes the order in which
|
| 742 |
block-scope objects with static and thread storage duration are
|
| 743 |
destroyed. — *end note*]
|
| 744 |
|
| 745 |
## Ambiguity resolution <a id="stmt.ambig">[[stmt.ambig]]</a>
|
| 746 |
|
| 747 |
There is an ambiguity in the grammar involving *expression-statement*s
|
| 748 |
and *declaration*s: An *expression-statement* with a function-style
|
| 749 |
-
explicit type conversion
|
| 750 |
subexpression can be indistinguishable from a *declaration* where the
|
| 751 |
first *declarator* starts with a `(`. In those cases the *statement* is
|
| 752 |
a *declaration*.
|
| 753 |
|
| 754 |
[*Note 1*:
|
|
@@ -758,11 +860,11 @@ ambiguity, so this rule does not apply. The whole *statement* might need
|
|
| 758 |
to be examined to determine whether this is the case. This resolves the
|
| 759 |
meaning of many examples.
|
| 760 |
|
| 761 |
[*Example 1*:
|
| 762 |
|
| 763 |
-
Assuming `T` is a *simple-type-specifier*
|
| 764 |
|
| 765 |
``` cpp
|
| 766 |
T(a)->m = 7; // expression-statement
|
| 767 |
T(a)++; // expression-statement
|
| 768 |
T(a,5)<<c; // expression-statement
|
|
@@ -838,32 +940,38 @@ void f() {
|
|
| 838 |
|
| 839 |
— *end example*]
|
| 840 |
|
| 841 |
<!-- Link reference definitions -->
|
| 842 |
[basic.def.odr]: basic.md#basic.def.odr
|
|
|
|
| 843 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 844 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 845 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 846 |
[basic.scope]: basic.md#basic.scope
|
|
|
|
| 847 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 848 |
[basic.start.main]: basic.md#basic.start.main
|
| 849 |
[basic.start.term]: basic.md#basic.start.term
|
| 850 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 851 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 852 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 853 |
-
[class.copy]:
|
| 854 |
-
[class.ctor]:
|
| 855 |
-
[class.dtor]:
|
| 856 |
-
[class.temporary]:
|
| 857 |
-
[conv]:
|
| 858 |
-
[conv.prom]:
|
|
|
|
|
|
|
| 859 |
[dcl.init]: dcl.md#dcl.init
|
| 860 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 861 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 862 |
[dcl.type]: dcl.md#dcl.type
|
| 863 |
-
[
|
|
|
|
| 864 |
[expr.const]: expr.md#expr.const
|
|
|
|
| 865 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 866 |
[stmt.ambig]: #stmt.ambig
|
| 867 |
[stmt.block]: #stmt.block
|
| 868 |
[stmt.break]: #stmt.break
|
| 869 |
[stmt.cont]: #stmt.cont
|
|
@@ -874,18 +982,20 @@ void f() {
|
|
| 874 |
[stmt.goto]: #stmt.goto
|
| 875 |
[stmt.if]: #stmt.if
|
| 876 |
[stmt.iter]: #stmt.iter
|
| 877 |
[stmt.jump]: #stmt.jump
|
| 878 |
[stmt.label]: #stmt.label
|
|
|
|
| 879 |
[stmt.ranged]: #stmt.ranged
|
| 880 |
[stmt.return]: #stmt.return
|
|
|
|
| 881 |
[stmt.select]: #stmt.select
|
| 882 |
[stmt.stmt]: #stmt.stmt
|
| 883 |
[stmt.switch]: #stmt.switch
|
| 884 |
[stmt.while]: #stmt.while
|
| 885 |
-
[support.start.term]:
|
| 886 |
-
[temp]: temp.md#temp
|
| 887 |
|
| 888 |
[^1]: In other words, the `else` is associated with the nearest un-elsed
|
| 889 |
`if`.
|
| 890 |
|
| 891 |
[^2]: The transfer from the condition of a `switch` statement to a
|
|
|
|
| 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
|
|
|
|
| 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 contained *statement*,
|
| 33 |
+
- for a *compound-statement*, any *statement* of its *statement-seq*,
|
| 34 |
+
- for a *selection-statement*, any of its *statement*s (but not its
|
| 35 |
+
*init-statement*), or
|
| 36 |
+
- for an *iteration-statement*, its contained *statement* (but not an
|
| 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` [[dcl.dcl]],
|
| 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 |
The rules for *condition*s apply both to *selection-statement*s and to
|
| 53 |
+
the `for` and `while` statements [[stmt.iter]]. A *condition* that is
|
| 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 |
+
[*Note 2*: A name introduced in a *selection-statement* or
|
| 61 |
+
*iteration-statement* outside of any substatement is in scope from its
|
| 62 |
+
point of declaration until the end of the statement’s substatements.
|
| 63 |
+
Such a name cannot be redeclared in the outermost block of any of the
|
| 64 |
+
substatements [[basic.scope.block]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 initialized declaration in a `switch` statement is the value
|
| 71 |
+
of the declared variable if it has integral or enumeration type, or of
|
| 72 |
+
that variable implicitly converted to integral or enumeration type
|
| 73 |
+
otherwise. The value of a *condition* that is an expression is the value
|
| 74 |
+
of the expression, contextually converted to `bool` for statements other
|
| 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 the declaration of a block-scope name, it is interpreted as a
|
| 81 |
declaration.
|
| 82 |
|
|
|
|
| 88 |
A statement can be labeled.
|
| 89 |
|
| 90 |
``` bnf
|
| 91 |
labeled-statement:
|
| 92 |
attribute-specifier-seqₒₚₜ identifier ':' statement
|
| 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. An
|
| 98 |
*identifier label* declares the identifier. The only use of an
|
| 99 |
identifier label is as the target of a `goto`. The scope of a label is
|
|
|
|
| 102 |
declaration. Labels have their own name space and do not interfere with
|
| 103 |
other identifiers.
|
| 104 |
|
| 105 |
[*Note 1*: A label may have the same name as another declaration in the
|
| 106 |
same scope or a *template-parameter* from an enclosing scope.
|
| 107 |
+
Unqualified name lookup [[basic.lookup.unqual]] ignores
|
| 108 |
labels. — *end note*]
|
| 109 |
|
| 110 |
+
Case labels and default labels shall occur only in `switch` statements.
|
| 111 |
|
| 112 |
## Expression statement <a id="stmt.expr">[[stmt.expr]]</a>
|
| 113 |
|
| 114 |
Expression statements have the form
|
| 115 |
|
| 116 |
``` bnf
|
| 117 |
expression-statement:
|
| 118 |
expressionₒₚₜ ';'
|
| 119 |
```
|
| 120 |
|
| 121 |
+
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 carry a
|
| 128 |
label just before the `}` of a compound statement and to supply a null
|
| 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 |
So that several statements can be used where one is expected, the
|
| 135 |
compound statement (also, and equivalently, called “block”) is provided.
|
|
|
|
| 143 |
statement-seq:
|
| 144 |
statement
|
| 145 |
statement-seq statement
|
| 146 |
```
|
| 147 |
|
| 148 |
+
A compound statement defines a block scope [[basic.scope]].
|
| 149 |
|
| 150 |
+
[*Note 1*: A declaration is a *statement* [[stmt.dcl]]. — *end note*]
|
|
|
|
| 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 |
+
The substatement in a *selection-statement* (each substatement, in the
|
| 169 |
+
`else` form of the `if` statement) implicitly defines a block scope
|
| 170 |
+
[[basic.scope]]. If the substatement in a *selection-statement* is a
|
| 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)
|
|
|
|
| 191 |
|
| 192 |
— *end example*]
|
| 193 |
|
| 194 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 195 |
|
| 196 |
+
If the condition [[stmt.select]] yields `true` the first substatement is
|
| 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 shall be a contextually converted constant expression of type
|
| 207 |
+
`bool` [[expr.const]]; this form is called a *constexpr if* statement.
|
| 208 |
+
If the value of the converted condition is `false`, the first
|
| 209 |
substatement is a *discarded statement*, otherwise the second
|
| 210 |
substatement, if present, is a discarded statement. During the
|
| 211 |
+
instantiation of an enclosing templated entity [[temp.pre]], if the
|
| 212 |
condition is not value-dependent after its instantiation, the discarded
|
| 213 |
substatement (if any) is not instantiated.
|
| 214 |
|
| 215 |
+
[*Note 1*: Odr-uses [[basic.def.odr]] in a discarded statement do not
|
| 216 |
+
require an entity to be defined. — *end note*]
|
| 217 |
|
| 218 |
A `case` or `default` label appearing within such an `if` statement
|
| 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) {
|
|
|
|
| 246 |
— *end example*]
|
| 247 |
|
| 248 |
An `if` statement of the form
|
| 249 |
|
| 250 |
``` bnf
|
| 251 |
+
if constexprₒₚₜ '(' init-statement condition ')' statement
|
| 252 |
```
|
| 253 |
|
| 254 |
is equivalent to
|
| 255 |
|
| 256 |
+
``` bnf
|
| 257 |
+
'{'
|
| 258 |
+
init-statement
|
| 259 |
+
if constexprₒₚₜ '(' condition ')' statement
|
| 260 |
+
'}'
|
| 261 |
+
```
|
| 262 |
+
|
| 263 |
and an `if` statement of the form
|
| 264 |
|
| 265 |
``` bnf
|
| 266 |
+
if constexprₒₚₜ '(' init-statement condition ')' statement else statement
|
| 267 |
```
|
| 268 |
|
| 269 |
is equivalent to
|
| 270 |
|
| 271 |
+
``` bnf
|
| 272 |
+
'{'
|
| 273 |
+
init-statement
|
| 274 |
+
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 275 |
+
'}'
|
| 276 |
+
```
|
| 277 |
+
|
| 278 |
except that names declared in the *init-statement* are in the same
|
| 279 |
declarative region as those declared in the *condition*.
|
| 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 condition shall be of integral type, enumeration type, or class
|
| 287 |
type. If of class type, the condition is contextually implicitly
|
| 288 |
+
converted [[conv]] to an integral or enumeration type. If the (possibly
|
| 289 |
+
converted) type is subject to integral promotions [[conv.prom]], the
|
| 290 |
+
condition is converted to the promoted type. Any statement within the
|
| 291 |
+
`switch` statement can be labeled with one or more case labels as
|
| 292 |
+
follows:
|
| 293 |
|
| 294 |
``` bnf
|
| 295 |
+
case constant-expression ':'
|
| 296 |
```
|
| 297 |
|
| 298 |
+
where the *constant-expression* shall be a converted constant expression
|
| 299 |
+
[[expr.const]] of the adjusted type of the switch condition. No two of
|
| 300 |
+
the case constants in the same switch shall have the same value after
|
| 301 |
+
conversion.
|
| 302 |
|
| 303 |
There shall be at most one label of the form
|
| 304 |
|
| 305 |
``` cpp
|
| 306 |
default :
|
|
|
|
| 309 |
within a `switch` statement.
|
| 310 |
|
| 311 |
Switch statements can be nested; a `case` or `default` label is
|
| 312 |
associated with the smallest switch enclosing it.
|
| 313 |
|
| 314 |
+
When the `switch` statement is executed, its condition is evaluated. If
|
| 315 |
+
one of the case constants has the same value as the condition, control
|
| 316 |
+
is passed to the statement following the matched case label. If no case
|
| 317 |
+
constant matches the condition, and if there is a `default` label,
|
| 318 |
+
control passes to the statement labeled by the default label. If no case
|
| 319 |
+
matches and if there is no `default` then none of the statements in the
|
| 320 |
+
switch is executed.
|
| 321 |
|
| 322 |
`case` and `default` labels in themselves do not alter the flow of
|
| 323 |
control, which continues unimpeded across such labels. To exit from a
|
| 324 |
+
switch, see `break`, [[stmt.break]].
|
| 325 |
|
| 326 |
[*Note 1*: Usually, the substatement that is the subject of a switch is
|
| 327 |
compound and `case` and `default` labels appear on the top-level
|
| 328 |
statements contained within the (compound) substatement, but this is not
|
| 329 |
required. Declarations can appear in the substatement of a `switch`
|
| 330 |
statement. — *end note*]
|
| 331 |
|
| 332 |
A `switch` statement of the form
|
| 333 |
|
| 334 |
``` bnf
|
| 335 |
+
switch '(' init-statement condition ')' statement
|
| 336 |
```
|
| 337 |
|
| 338 |
is equivalent to
|
| 339 |
|
| 340 |
+
``` bnf
|
| 341 |
+
'{'
|
| 342 |
+
init-statement
|
| 343 |
+
switch '(' condition ')' statement
|
| 344 |
+
'}'
|
| 345 |
+
```
|
| 346 |
+
|
| 347 |
except that names declared in the *init-statement* are in the same
|
| 348 |
declarative region as those declared in the *condition*.
|
| 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
|
| 357 |
+
do statement while '(' expression ')' ';'
|
| 358 |
+
for '(' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 359 |
+
for '(' init-statementₒₚₜ for-range-declaration ':' for-range-initializer ')' statement
|
| 360 |
```
|
| 361 |
|
| 362 |
``` bnf
|
| 363 |
for-range-declaration:
|
| 364 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
|
|
|
| 374 |
*for-range-declaration*.
|
| 375 |
|
| 376 |
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 377 |
|
| 378 |
The substatement in an *iteration-statement* implicitly defines a block
|
| 379 |
+
scope [[basic.scope]] which is entered and exited each time through the
|
| 380 |
+
loop. If the substatement in an *iteration-statement* is a single
|
| 381 |
+
statement and not a *compound-statement*, it is as if it was rewritten
|
| 382 |
+
to be a *compound-statement* containing the original statement.
|
|
|
|
|
|
|
| 383 |
|
| 384 |
[*Example 1*:
|
| 385 |
|
| 386 |
``` cpp
|
| 387 |
while (--x >= 0)
|
|
|
|
| 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.select]] becomes `false`. The test
|
| 424 |
takes place before each execution of the substatement.
|
| 425 |
|
| 426 |
When the condition of a `while` statement is a declaration, the scope of
|
| 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 ')' '{'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
statement
|
| 436 |
+
goto label ';'
|
| 437 |
+
'}'
|
| 438 |
+
'}'
|
| 439 |
```
|
| 440 |
|
| 441 |
+
[*Note 1*:
|
| 442 |
+
|
| 443 |
+
The variable created in the condition is destroyed and created with each
|
| 444 |
iteration of the loop.
|
| 445 |
|
| 446 |
[*Example 1*:
|
| 447 |
|
| 448 |
``` cpp
|
|
|
|
| 463 |
once for the condition that succeeds and once for the condition that
|
| 464 |
fails.
|
| 465 |
|
| 466 |
— *end example*]
|
| 467 |
|
| 468 |
+
— *end note*]
|
| 469 |
+
|
| 470 |
### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
|
| 471 |
|
| 472 |
+
The expression is contextually converted to `bool` [[conv]]; if that
|
| 473 |
+
conversion is ill-formed, the program is ill-formed.
|
| 474 |
|
| 475 |
In the `do` statement the substatement is executed repeatedly until the
|
| 476 |
value of the expression becomes `false`. The test takes place after each
|
| 477 |
execution of the statement.
|
| 478 |
|
| 479 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 480 |
|
| 481 |
The `for` statement
|
| 482 |
|
| 483 |
``` bnf
|
| 484 |
+
for '(' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 485 |
```
|
| 486 |
|
| 487 |
is equivalent to
|
| 488 |
|
| 489 |
+
``` bnf
|
| 490 |
+
'{'
|
| 491 |
+
init-statement
|
| 492 |
+
while '(' condition ')' '{'
|
| 493 |
+
statement
|
| 494 |
+
expression ';'
|
| 495 |
+
'}'
|
| 496 |
+
'}'
|
| 497 |
+
```
|
| 498 |
+
|
| 499 |
except that names declared in the *init-statement* are in the same
|
| 500 |
declarative region as those declared in the *condition*, and except that
|
| 501 |
a `continue` in *statement* (not enclosed in another iteration
|
| 502 |
statement) will execute *expression* before re-evaluating *condition*.
|
| 503 |
|
| 504 |
[*Note 1*: Thus the first statement specifies initialization for the
|
| 505 |
+
loop; the condition [[stmt.select]] specifies a test, sequenced before
|
| 506 |
+
each iteration, such that the loop is exited when the condition becomes
|
| 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 |
|
|
|
|
| 531 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 532 |
|
| 533 |
The range-based `for` statement
|
| 534 |
|
| 535 |
``` bnf
|
| 536 |
+
for '(' init-statementₒₚₜ for-range-declaration ':' for-range-initializer ')' statement
|
| 537 |
```
|
| 538 |
|
| 539 |
is equivalent to
|
| 540 |
|
| 541 |
+
``` bnf
|
| 542 |
+
'{'
|
| 543 |
+
init-statementₒₚₜ
|
| 544 |
+
auto '&&'range '=' for-range-initializer ';'
|
| 545 |
+
auto begin '=' begin-expr ';'
|
| 546 |
+
auto end '=' end-expr ';'
|
| 547 |
+
for '(' ';' begin '!=' end';' '++'begin ')' '{'
|
| 548 |
+
for-range-declaration '=' '*' begin ';'
|
| 549 |
+
statement
|
| 550 |
+
'}'
|
| 551 |
+
'}'
|
| 552 |
+
```
|
| 553 |
+
|
| 554 |
where
|
| 555 |
|
| 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 |
the *unqualified-id*s `begin` and `end` are looked up in the scope
|
| 569 |
+
of `C` as if by class member access lookup
|
| 570 |
+
[[basic.lookup.classref]], and if both find at least one
|
| 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` are looked up in
|
| 575 |
+
the associated namespaces [[basic.lookup.argdep]].
|
| 576 |
+
\[*Note 1*: Ordinary unqualified lookup [[basic.lookup.unqual]] is
|
| 577 |
+
not performed. — *end note*]
|
| 578 |
|
| 579 |
[*Example 1*:
|
| 580 |
|
| 581 |
``` cpp
|
| 582 |
int array[5] = { 1, 2, 3, 4, 5 };
|
|
|
|
| 594 |
|
| 595 |
Jump statements unconditionally transfer control.
|
| 596 |
|
| 597 |
``` bnf
|
| 598 |
jump-statement:
|
| 599 |
+
break ';'
|
| 600 |
+
continue ';'
|
| 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 automatic
|
| 607 |
+
storage duration [[basic.stc.auto]] that have been constructed in that
|
| 608 |
+
scope are destroyed in the reverse order of their construction.
|
| 609 |
|
| 610 |
[*Note 1*: For temporaries, see [[class.temporary]]. — *end note*]
|
| 611 |
|
| 612 |
Transfer out of a loop, out of a block, or back past an initialized
|
| 613 |
variable with automatic storage duration involves the destruction of
|
| 614 |
objects with automatic storage duration that are in scope at the point
|
| 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 |
The `break` statement shall occur only in an *iteration-statement* or a
|
| 629 |
`switch` statement and causes termination of the smallest enclosing
|
| 630 |
*iteration-statement* or `switch` statement; control passes to the
|
|
|
|
| 663 |
contin: ;
|
| 664 |
}
|
| 665 |
```
|
| 666 |
|
| 667 |
a `continue` not contained in an enclosed iteration statement is
|
| 668 |
+
equivalent to `goto` *`contin`*.
|
| 669 |
|
| 670 |
### The `return` statement <a id="stmt.return">[[stmt.return]]</a>
|
| 671 |
|
| 672 |
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 whose return type is
|
| 679 |
+
cv `void`. A `return` statement with any other operand shall be used
|
| 680 |
+
only in a function whose return type is not cv `void`; the `return`
|
| 681 |
+
statement initializes the glvalue result or prvalue result object of the
|
| 682 |
+
(explicit or implicit) function call by copy-initialization [[dcl.init]]
|
| 683 |
+
from the operand.
|
| 684 |
|
| 685 |
+
[*Note 1*: A `return` statement can involve an invocation of 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 may be elided or
|
| 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) {
|
|
|
|
| 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)
|
| 712 |
+
```
|
| 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 other than
|
| 719 |
+
`main` [[basic.start.main]] or a coroutine [[dcl.fct.def.coroutine]]
|
| 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
|
| 725 |
+
sequenced before the destruction of local variables [[stmt.jump]] of the
|
| 726 |
+
block enclosing the `return` statement.
|
| 727 |
+
|
| 728 |
+
### The `co_return` statement <a id="stmt.return.coroutine">[[stmt.return.coroutine]]</a>
|
| 729 |
+
|
| 730 |
+
``` bnf
|
| 731 |
+
coroutine-return-statement:
|
| 732 |
+
'co_return' expr-or-braced-init-listₒₚₜ ';'
|
| 733 |
+
```
|
| 734 |
+
|
| 735 |
+
A coroutine returns to its caller or resumer [[dcl.fct.def.coroutine]]
|
| 736 |
+
by the `co_return` statement or when suspended [[expr.await]]. A
|
| 737 |
+
coroutine shall not enclose a `return` statement [[stmt.return]].
|
| 738 |
+
|
| 739 |
+
[*Note 1*: For this determination, it is irrelevant whether the
|
| 740 |
+
`return` statement is enclosed by a discarded statement
|
| 741 |
+
[[stmt.if]]. — *end note*]
|
| 742 |
+
|
| 743 |
+
The *expr-or-braced-init-list* of a `co_return` statement is called its
|
| 744 |
+
operand. Let *p* be an lvalue naming the coroutine promise object
|
| 745 |
+
[[dcl.fct.def.coroutine]]. A `co_return` statement is equivalent to:
|
| 746 |
+
|
| 747 |
+
``` bnf
|
| 748 |
+
'{' S';' 'goto' final-suspend';' '}'
|
| 749 |
+
```
|
| 750 |
+
|
| 751 |
+
where *`final-suspend`* is the exposition-only label defined in
|
| 752 |
+
[[dcl.fct.def.coroutine]] and *S* is defined as follows:
|
| 753 |
+
|
| 754 |
+
- If the operand is a *braced-init-list* or an expression of non-`void`
|
| 755 |
+
type, *S* is *p*`.return_value(`*expr-or-braced-init-list*`)`. The
|
| 756 |
+
expression *S* shall be a prvalue of type `void`.
|
| 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 operand; otherwise
|
| 763 |
+
flowing off the end of a coroutine results in undefined behavior.
|
| 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 identifiers into a
|
| 774 |
block; it has the form
|
|
|
|
| 780 |
|
| 781 |
If an identifier introduced by a declaration was previously declared in
|
| 782 |
an outer block, the outer declaration is hidden for the remainder of the
|
| 783 |
block, after which it resumes its force.
|
| 784 |
|
| 785 |
+
Variables with automatic storage duration [[basic.stc.auto]] are
|
| 786 |
initialized each time their *declaration-statement* is executed.
|
| 787 |
Variables with automatic storage duration declared in the block are
|
| 788 |
+
destroyed on exit from the block [[stmt.jump]].
|
| 789 |
|
| 790 |
It is possible to transfer into a block, but not in a way that bypasses
|
| 791 |
+
declarations with initialization (including ones in *condition*s and
|
| 792 |
+
*init-statement*s). A program that jumps[^2] from a point where a
|
| 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() {
|
| 803 |
// ...
|
| 804 |
+
goto lx; // error: jump into scope of a
|
| 805 |
// ...
|
| 806 |
ly:
|
| 807 |
X a = 1;
|
| 808 |
// ...
|
| 809 |
lx:
|
|
|
|
| 813 |
```
|
| 814 |
|
| 815 |
— *end example*]
|
| 816 |
|
| 817 |
Dynamic initialization of a block-scope variable with static storage
|
| 818 |
+
duration [[basic.stc.static]] or thread storage duration
|
| 819 |
+
[[basic.stc.thread]] is performed the first time control passes through
|
| 820 |
its declaration; such a variable is considered initialized upon the
|
| 821 |
completion of its initialization. If the initialization exits by
|
| 822 |
throwing an exception, the initialization is not complete, so it will be
|
| 823 |
tried again the next time control enters the declaration. If control
|
| 824 |
enters the declaration concurrently while the variable is being
|
|
|
|
| 828 |
|
| 829 |
[*Example 2*:
|
| 830 |
|
| 831 |
``` cpp
|
| 832 |
int foo(int i) {
|
| 833 |
+
static int s = foo(2*i); // undefined behavior: recursive call
|
| 834 |
return i+1;
|
| 835 |
}
|
| 836 |
```
|
| 837 |
|
| 838 |
— *end example*]
|
| 839 |
|
| 840 |
+
A block-scope object with static or thread storage duration will be
|
| 841 |
+
destroyed if and only if it was constructed.
|
| 842 |
|
| 843 |
[*Note 1*: [[basic.start.term]] describes the order in which
|
| 844 |
block-scope objects with static and thread storage duration are
|
| 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
|
| 851 |
+
explicit type conversion [[expr.type.conv]] as its leftmost
|
| 852 |
subexpression can be indistinguishable from a *declaration* where the
|
| 853 |
first *declarator* starts with a `(`. In those cases the *statement* is
|
| 854 |
a *declaration*.
|
| 855 |
|
| 856 |
[*Note 1*:
|
|
|
|
| 860 |
to be examined to determine whether this is the case. This resolves the
|
| 861 |
meaning of many examples.
|
| 862 |
|
| 863 |
[*Example 1*:
|
| 864 |
|
| 865 |
+
Assuming `T` is a *simple-type-specifier* [[dcl.type]],
|
| 866 |
|
| 867 |
``` cpp
|
| 868 |
T(a)->m = 7; // expression-statement
|
| 869 |
T(a)++; // expression-statement
|
| 870 |
T(a,5)<<c; // expression-statement
|
|
|
|
| 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
|
| 965 |
[dcl.init]: dcl.md#dcl.init
|
| 966 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 967 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 968 |
[dcl.type]: dcl.md#dcl.type
|
| 969 |
+
[except.ctor]: except.md#except.ctor
|
| 970 |
+
[expr.await]: expr.md#expr.await
|
| 971 |
[expr.const]: expr.md#expr.const
|
| 972 |
+
[expr.context]: expr.md#expr.context
|
| 973 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 974 |
[stmt.ambig]: #stmt.ambig
|
| 975 |
[stmt.block]: #stmt.block
|
| 976 |
[stmt.break]: #stmt.break
|
| 977 |
[stmt.cont]: #stmt.cont
|
|
|
|
| 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
|