- tmp/tmp7bm7hd15/{from.md → to.md} +351 -217
tmp/tmp7bm7hd15/{from.md → to.md}
RENAMED
|
@@ -10,15 +10,71 @@ statement:
|
|
| 10 |
attribute-specifier-seqₒₚₜ selection-statement
|
| 11 |
attribute-specifier-seqₒₚₜ iteration-statement
|
| 12 |
attribute-specifier-seqₒₚₜ jump-statement
|
| 13 |
declaration-statement
|
| 14 |
attribute-specifier-seqₒₚₜ try-block
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
```
|
| 16 |
|
| 17 |
The optional *attribute-specifier-seq* appertains to the respective
|
| 18 |
statement.
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
## Labeled statement <a id="stmt.label">[[stmt.label]]</a>
|
| 21 |
|
| 22 |
A statement can be labeled.
|
| 23 |
|
| 24 |
``` bnf
|
|
@@ -27,15 +83,21 @@ labeled-statement:
|
|
| 27 |
attribute-specifier-seqₒₚₜ 'case' constant-expression ':' statement
|
| 28 |
attribute-specifier-seqₒₚₜ 'default :' statement
|
| 29 |
```
|
| 30 |
|
| 31 |
The optional *attribute-specifier-seq* appertains to the label. An
|
| 32 |
-
identifier label declares the identifier. The only use of an
|
| 33 |
-
label is as the target of a `goto`. The scope of a label is
|
| 34 |
-
in which it appears. Labels shall not be redeclared within
|
| 35 |
-
label can be used in a `goto` statement before its
|
| 36 |
-
have their own name space and do not interfere with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
Case labels and default labels shall occur only in switch statements.
|
| 39 |
|
| 40 |
## Expression statement <a id="stmt.expr">[[stmt.expr]]</a>
|
| 41 |
|
|
@@ -47,15 +109,17 @@ expression-statement:
|
|
| 47 |
```
|
| 48 |
|
| 49 |
The expression is a discarded-value expression (Clause [[expr]]). All
|
| 50 |
side effects from an expression statement are completed before the next
|
| 51 |
statement is executed. An expression statement with the expression
|
| 52 |
-
missing is called a null statement.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
statement
|
|
|
|
|
|
|
| 57 |
|
| 58 |
## Compound statement or block <a id="stmt.block">[[stmt.block]]</a>
|
| 59 |
|
| 60 |
So that several statements can be used where one is expected, the
|
| 61 |
compound statement (also, and equivalently, called “block”) is provided.
|
|
@@ -69,41 +133,42 @@ compound-statement:
|
|
| 69 |
statement-seq:
|
| 70 |
statement
|
| 71 |
statement-seq statement
|
| 72 |
```
|
| 73 |
|
| 74 |
-
A compound statement defines a block scope ([[basic.scope]]).
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
## Selection statements <a id="stmt.select">[[stmt.select]]</a>
|
| 78 |
|
| 79 |
Selection statements choose one of several flows of control.
|
| 80 |
|
| 81 |
``` bnf
|
| 82 |
selection-statement:
|
| 83 |
-
'if (' condition ')' statement
|
| 84 |
-
'if (' condition ')' statement 'else' statement
|
| 85 |
-
'switch (' condition ')' statement
|
| 86 |
-
```
|
| 87 |
-
|
| 88 |
-
``` bnf
|
| 89 |
-
condition:
|
| 90 |
-
expression
|
| 91 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
|
| 92 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator braced-init-list
|
| 93 |
```
|
| 94 |
|
| 95 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 96 |
-
condition.
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
notation. The substatement in a *selection-statement* (each
|
| 99 |
substatement, in the `else` form of the `if` statement) implicitly
|
| 100 |
defines a block scope ([[basic.scope]]). If the substatement in a
|
| 101 |
selection-statement is a single statement and not a
|
| 102 |
-
*compound-statement
|
| 103 |
compound-statement containing the original substatement.
|
| 104 |
|
|
|
|
|
|
|
| 105 |
``` cpp
|
| 106 |
if (x)
|
| 107 |
int i;
|
| 108 |
```
|
| 109 |
|
|
@@ -115,52 +180,11 @@ if (x) {
|
|
| 115 |
}
|
| 116 |
```
|
| 117 |
|
| 118 |
Thus after the `if` statement, `i` is no longer in scope.
|
| 119 |
|
| 120 |
-
|
| 121 |
-
the `for` and `while` statements ([[stmt.iter]]). The *declarator*
|
| 122 |
-
shall not specify a function or an array. The *decl-specifier-seq* shall
|
| 123 |
-
not define a class or enumeration. If the `auto` appears in the , the
|
| 124 |
-
type of the identifier being declared is deduced from the initializer as
|
| 125 |
-
described in [[dcl.spec.auto]].
|
| 126 |
-
|
| 127 |
-
A name introduced by a declaration in a *condition* (either introduced
|
| 128 |
-
by the *decl-specifier-seq* or the *declarator* of the condition) is in
|
| 129 |
-
scope from its point of declaration until the end of the substatements
|
| 130 |
-
controlled by the condition. If the name is re-declared in the outermost
|
| 131 |
-
block of a substatement controlled by the condition, the declaration
|
| 132 |
-
that re-declares the name is ill-formed.
|
| 133 |
-
|
| 134 |
-
``` cpp
|
| 135 |
-
if (int x = f()) {
|
| 136 |
-
int x; // ill-formed, redeclaration of x
|
| 137 |
-
}
|
| 138 |
-
else {
|
| 139 |
-
int x; // ill-formed, redeclaration of x
|
| 140 |
-
}
|
| 141 |
-
```
|
| 142 |
-
|
| 143 |
-
The value of a *condition* that is an initialized declaration in a
|
| 144 |
-
statement other than a `switch` statement is the value of the declared
|
| 145 |
-
variable contextually converted to `bool` (Clause [[conv]]). If that
|
| 146 |
-
conversion is ill-formed, the program is ill-formed. The value of a
|
| 147 |
-
*condition* that is an initialized declaration in a `switch` statement
|
| 148 |
-
is the value of the declared variable if it has integral or enumeration
|
| 149 |
-
type, or of that variable implicitly converted to integral or
|
| 150 |
-
enumeration type otherwise. The value of a *condition* that is an
|
| 151 |
-
expression is the value of the expression, contextually converted to
|
| 152 |
-
`bool` for statements other than `switch`; if that conversion is
|
| 153 |
-
ill-formed, the program is ill-formed. The value of the condition will
|
| 154 |
-
be referred to as simply “the condition” where the usage is unambiguous.
|
| 155 |
-
|
| 156 |
-
If a *condition* can be syntactically resolved as either an expression
|
| 157 |
-
or the declaration of a block-scope name, it is interpreted as a
|
| 158 |
-
declaration.
|
| 159 |
-
|
| 160 |
-
In the *decl-specifier-seq* of a *condition*, each *decl-specifier*
|
| 161 |
-
shall be either a *type-specifier* or `constexpr`.
|
| 162 |
|
| 163 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 164 |
|
| 165 |
If the condition ([[stmt.select]]) yields `true` the first substatement
|
| 166 |
is executed. If the `else` part of the selection statement is present
|
|
@@ -169,10 +193,72 @@ If the first substatement is reached via a label, the condition is not
|
|
| 169 |
evaluated and the second substatement is not executed. In the second
|
| 170 |
form of `if` statement (the one including `else`), if the first
|
| 171 |
substatement is also an `if` statement then that inner `if` statement
|
| 172 |
shall contain an `else` part.[^1]
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 175 |
|
| 176 |
The `switch` statement causes control to be transferred to one of
|
| 177 |
several statements depending on the value of a condition.
|
| 178 |
|
|
@@ -212,56 +298,67 @@ condition, and if there is a `default` label, control passes to the
|
|
| 212 |
statement labeled by the default label. If no case matches and if there
|
| 213 |
is no `default` then none of the statements in the switch is executed.
|
| 214 |
|
| 215 |
`case` and `default` labels in themselves do not alter the flow of
|
| 216 |
control, which continues unimpeded across such labels. To exit from a
|
| 217 |
-
switch, see `break`, [[stmt.break]].
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
substatement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
|
| 224 |
|
| 225 |
Iteration statements specify looping.
|
| 226 |
|
| 227 |
``` bnf
|
| 228 |
iteration-statement:
|
| 229 |
'while (' condition ')' statement
|
| 230 |
'do' statement 'while (' expression ') ;'
|
| 231 |
-
'for ('
|
| 232 |
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 233 |
```
|
| 234 |
|
| 235 |
-
``` bnf
|
| 236 |
-
for-init-statement:
|
| 237 |
-
expression-statement
|
| 238 |
-
simple-declaration
|
| 239 |
-
```
|
| 240 |
-
|
| 241 |
``` bnf
|
| 242 |
for-range-declaration:
|
| 243 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
|
|
|
| 244 |
```
|
| 245 |
|
| 246 |
``` bnf
|
| 247 |
for-range-initializer:
|
| 248 |
-
|
| 249 |
-
braced-init-list
|
| 250 |
```
|
| 251 |
|
| 252 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 253 |
-
*for-range-declaration*.
|
|
|
|
|
|
|
| 254 |
|
| 255 |
The substatement in an *iteration-statement* implicitly defines a block
|
| 256 |
scope ([[basic.scope]]) which is entered and exited each time through
|
| 257 |
the loop.
|
| 258 |
|
| 259 |
If the substatement in an iteration-statement is a single statement and
|
| 260 |
-
not a *compound-statement
|
| 261 |
compound-statement containing the original statement.
|
| 262 |
|
|
|
|
|
|
|
| 263 |
``` cpp
|
| 264 |
while (--x >= 0)
|
| 265 |
int i;
|
| 266 |
```
|
| 267 |
|
|
@@ -273,12 +370,28 @@ while (--x >= 0) {
|
|
| 273 |
}
|
| 274 |
```
|
| 275 |
|
| 276 |
Thus after the `while` statement, `i` is no longer in scope.
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
|
| 282 |
|
| 283 |
In the `while` statement the substatement is executed repeatedly until
|
| 284 |
the value of the condition ([[stmt.select]]) becomes `false`. The test
|
|
@@ -307,10 +420,12 @@ label:
|
|
| 307 |
```
|
| 308 |
|
| 309 |
The variable created in a condition is destroyed and created with each
|
| 310 |
iteration of the loop.
|
| 311 |
|
|
|
|
|
|
|
| 312 |
``` cpp
|
| 313 |
struct A {
|
| 314 |
int val;
|
| 315 |
A(int i) : val(i) { }
|
| 316 |
~A() { }
|
|
@@ -325,10 +440,12 @@ while (A a = i) {
|
|
| 325 |
|
| 326 |
In the while-loop, the constructor and destructor are each called twice,
|
| 327 |
once for the condition that succeeds and once for the condition that
|
| 328 |
fails.
|
| 329 |
|
|
|
|
|
|
|
| 330 |
### The `do` statement <a id="stmt.do">[[stmt.do]]</a>
|
| 331 |
|
| 332 |
The expression is contextually converted to `bool` (Clause [[conv]]);
|
| 333 |
if that conversion is ill-formed, the program is ill-formed.
|
| 334 |
|
|
@@ -339,103 +456,92 @@ execution of the statement.
|
|
| 339 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 340 |
|
| 341 |
The `for` statement
|
| 342 |
|
| 343 |
``` bnf
|
| 344 |
-
'for ('
|
| 345 |
```
|
| 346 |
|
| 347 |
is equivalent to
|
| 348 |
|
| 349 |
-
except that names declared in the *
|
| 350 |
declarative region as those declared in the *condition*, and except that
|
| 351 |
a `continue` in *statement* (not enclosed in another iteration
|
| 352 |
statement) will execute *expression* before re-evaluating *condition*.
|
| 353 |
-
Thus the first statement specifies initialization for the loop; the
|
| 354 |
-
condition ([[stmt.select]]) specifies a test, made before each
|
| 355 |
-
iteration, such that the loop is exited when the condition becomes
|
| 356 |
-
`false`; the expression often specifies incrementing that is done after
|
| 357 |
-
each iteration.
|
| 358 |
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
missing *condition* makes the implied `while` clause equivalent to
|
| 361 |
`while(true)`.
|
| 362 |
|
| 363 |
-
If the *
|
| 364 |
declared extends to the end of the `for` statement.
|
| 365 |
|
|
|
|
|
|
|
| 366 |
``` cpp
|
| 367 |
int i = 42;
|
| 368 |
int a[10];
|
| 369 |
|
| 370 |
for (int i = 0; i < 10; i++)
|
| 371 |
a[i] = i;
|
| 372 |
|
| 373 |
int j = i; // j = 42
|
| 374 |
```
|
| 375 |
|
|
|
|
|
|
|
| 376 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 377 |
|
| 378 |
-
|
| 379 |
|
| 380 |
``` bnf
|
| 381 |
-
'for (' for-range-declaration :
|
| 382 |
```
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
```
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
```
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
__begin != __end;
|
| 406 |
-
++__begin ) {
|
| 407 |
-
for-range-declaration = *__begin;
|
| 408 |
-
statement
|
| 409 |
-
}
|
| 410 |
-
}
|
| 411 |
-
```
|
| 412 |
-
|
| 413 |
-
where `__range`, `__begin`, and `__end` are variables defined for
|
| 414 |
-
exposition only, and `_RangeT` is the type of the *expression*, and
|
| 415 |
-
*begin-expr* and *end-expr* are determined as follows:
|
| 416 |
-
|
| 417 |
-
- if `_RangeT` is an array type, *begin-expr* and *end-expr* are
|
| 418 |
-
`__range` and `__range + __bound`, respectively, where `__bound` is
|
| 419 |
-
the array bound. If `_RangeT` is an array of unknown size or an array
|
| 420 |
-
of incomplete type, the program is ill-formed;
|
| 421 |
-
- if `_RangeT` is a class type, the *unqualified-id*s `begin` and `end`
|
| 422 |
-
are looked up in the scope of class `\mbox{_RangeT}` as if by class
|
| 423 |
-
member access lookup ([[basic.lookup.classref]]), and if either (or
|
| 424 |
-
both) finds at least one declaration, *begin-expr* and *end-expr* are
|
| 425 |
-
`__range.begin()` and `__range.end()`, respectively;
|
| 426 |
- otherwise, *begin-expr* and *end-expr* are `begin(__range)` and
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
``` cpp
|
| 432 |
int array[5] = { 1, 2, 3, 4, 5 };
|
| 433 |
for (int& x : array)
|
| 434 |
x *= 2;
|
| 435 |
```
|
| 436 |
|
|
|
|
|
|
|
| 437 |
In the *decl-specifier-seq* of a *for-range-declaration*, each
|
| 438 |
*decl-specifier* shall be either a *type-specifier* or `constexpr`. The
|
| 439 |
*decl-specifier-seq* shall not define a class or enumeration.
|
| 440 |
|
| 441 |
## Jump statements <a id="stmt.jump">[[stmt.jump]]</a>
|
|
@@ -444,26 +550,30 @@ Jump statements unconditionally transfer control.
|
|
| 444 |
|
| 445 |
``` bnf
|
| 446 |
jump-statement:
|
| 447 |
'break ;'
|
| 448 |
'continue ;'
|
| 449 |
-
'return'
|
| 450 |
-
'return' braced-init-list ';'
|
| 451 |
'goto' identifier ';'
|
| 452 |
```
|
| 453 |
|
| 454 |
On exit from a scope (however accomplished), objects with automatic
|
| 455 |
storage duration ([[basic.stc.auto]]) that have been constructed in
|
| 456 |
-
that scope are destroyed in the reverse order of their construction.
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
### The `break` statement <a id="stmt.break">[[stmt.break]]</a>
|
| 467 |
|
| 468 |
The `break` statement shall occur only in an *iteration-statement* or a
|
| 469 |
`switch` statement and causes termination of the smallest enclosing
|
|
@@ -509,38 +619,48 @@ equivalent to `goto` `contin`.
|
|
| 509 |
|
| 510 |
### The `return` statement <a id="stmt.return">[[stmt.return]]</a>
|
| 511 |
|
| 512 |
A function returns to its caller by the `return` statement.
|
| 513 |
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
function
|
| 517 |
[[class.ctor]]), or a destructor ([[class.dtor]]). A return statement
|
| 518 |
-
with an
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
[[
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
|
| 530 |
``` cpp
|
| 531 |
std::pair<std::string,int> f(const char* p, int x) {
|
| 532 |
return {p,x};
|
| 533 |
}
|
| 534 |
```
|
| 535 |
|
| 536 |
-
|
| 537 |
-
value; this results in undefined behavior in a value-returning function.
|
| 538 |
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
|
| 543 |
### The `goto` statement <a id="stmt.goto">[[stmt.goto]]</a>
|
| 544 |
|
| 545 |
The `goto` statement unconditionally transfers control to the statement
|
| 546 |
labeled by the identifier. The identifier shall be a label (
|
|
@@ -564,80 +684,85 @@ Variables with automatic storage duration ([[basic.stc.auto]]) are
|
|
| 564 |
initialized each time their *declaration-statement* is executed.
|
| 565 |
Variables with automatic storage duration declared in the block are
|
| 566 |
destroyed on exit from the block ([[stmt.jump]]).
|
| 567 |
|
| 568 |
It is possible to transfer into a block, but not in a way that bypasses
|
| 569 |
-
declarations with initialization. A program that jumps[^
|
| 570 |
where a variable with automatic storage duration is not in scope to a
|
| 571 |
point where it is in scope is ill-formed unless the variable has scalar
|
| 572 |
type, class type with a trivial default constructor and a trivial
|
| 573 |
destructor, a cv-qualified version of one of these types, or an array of
|
| 574 |
one of the preceding types and is declared without an *initializer* (
|
| 575 |
[[dcl.init]]).
|
| 576 |
|
|
|
|
|
|
|
| 577 |
``` cpp
|
| 578 |
void f() {
|
| 579 |
// ...
|
| 580 |
goto lx; // ill-formed: jump into scope of a
|
| 581 |
// ...
|
| 582 |
ly:
|
| 583 |
X a = 1;
|
| 584 |
// ...
|
| 585 |
lx:
|
| 586 |
-
goto ly; // OK, jump implies destructor
|
| 587 |
-
//
|
| 588 |
-
// again immediately following label ly
|
| 589 |
}
|
| 590 |
```
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
[[basic.
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
time control enters the declaration. If control enters the declaration
|
| 608 |
-
concurrently while the variable is being initialized, the concurrent
|
| 609 |
-
execution shall wait for completion of the initialization.[^4] If
|
| 610 |
-
control re-enters the declaration recursively while the variable is
|
| 611 |
-
being initialized, the behavior is undefined.
|
| 612 |
|
| 613 |
``` cpp
|
| 614 |
int foo(int i) {
|
| 615 |
static int s = foo(2*i); // recursive call - undefined
|
| 616 |
return i+1;
|
| 617 |
}
|
| 618 |
```
|
| 619 |
|
|
|
|
|
|
|
| 620 |
The destructor for a block-scope object with static or thread storage
|
| 621 |
duration will be executed if and only if it was constructed.
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
| 624 |
|
| 625 |
## Ambiguity resolution <a id="stmt.ambig">[[stmt.ambig]]</a>
|
| 626 |
|
| 627 |
There is an ambiguity in the grammar involving *expression-statement*s
|
| 628 |
and *declaration*s: An *expression-statement* with a function-style
|
| 629 |
explicit type conversion ([[expr.type.conv]]) as its leftmost
|
| 630 |
subexpression can be indistinguishable from a *declaration* where the
|
| 631 |
first *declarator* starts with a `(`. In those cases the *statement* is
|
| 632 |
a *declaration*.
|
| 633 |
|
|
|
|
|
|
|
| 634 |
If the *statement* cannot syntactically be a *declaration*, there is no
|
| 635 |
ambiguity, so this rule does not apply. The whole *statement* might need
|
| 636 |
to be examined to determine whether this is the case. This resolves the
|
| 637 |
-
meaning of many examples.
|
| 638 |
-
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
``` cpp
|
| 641 |
T(a)->m = 7; // expression-statement
|
| 642 |
T(a)++; // expression-statement
|
| 643 |
T(a,5)<<c; // expression-statement
|
|
@@ -650,12 +775,16 @@ T(*g)(double(3)); // declaration
|
|
| 650 |
|
| 651 |
In the last example above, `g`, which is a pointer to `T`, is
|
| 652 |
initialized to `double(3)`. This is of course ill-formed for semantic
|
| 653 |
reasons, but that does not affect the syntactic analysis.
|
| 654 |
|
|
|
|
|
|
|
| 655 |
The remaining cases are *declaration*s.
|
| 656 |
|
|
|
|
|
|
|
| 657 |
``` cpp
|
| 658 |
class T {
|
| 659 |
// ...
|
| 660 |
public:
|
| 661 |
T();
|
|
@@ -668,20 +797,28 @@ T(c)=7; // declaration
|
|
| 668 |
T(d),e,f=3; // declaration
|
| 669 |
extern int h;
|
| 670 |
T(g)(h,2); // declaration
|
| 671 |
```
|
| 672 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
The disambiguation is purely syntactic; that is, the meaning of the
|
| 674 |
names occurring in such a statement, beyond whether they are
|
| 675 |
*type-name*s or not, is not generally used in or changed by the
|
| 676 |
disambiguation. Class templates are instantiated as necessary to
|
| 677 |
determine if a qualified name is a *type-name*. Disambiguation precedes
|
| 678 |
parsing, and a statement disambiguated as a declaration may be an
|
| 679 |
ill-formed declaration. If, during parsing, a name in a template
|
| 680 |
parameter is bound differently than it would be bound during a trial
|
| 681 |
-
parse, the program is ill-formed. No diagnostic is required.
|
| 682 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
|
| 684 |
``` cpp
|
| 685 |
struct T1 {
|
| 686 |
T1 operator()(int x) { return T1(x); }
|
| 687 |
int operator=(int x) { return x; }
|
|
@@ -691,27 +828,26 @@ struct T2 { T2(int){ } };
|
|
| 691 |
int a, (*(*b)(T2))(int), c, d;
|
| 692 |
|
| 693 |
void f() {
|
| 694 |
// disambiguation requires this to be parsed as a declaration:
|
| 695 |
T1(a) = 3,
|
| 696 |
-
T2(4), // T2 will be declared as
|
| 697 |
-
(*(*b)(T2(c)))(int(d)); //
|
| 698 |
-
//
|
| 699 |
-
// the last part of the
|
| 700 |
-
// declaration to parse
|
| 701 |
-
// properly since it depends
|
| 702 |
-
// on T2 being a type-name
|
| 703 |
}
|
| 704 |
```
|
| 705 |
|
|
|
|
|
|
|
| 706 |
<!-- Link reference definitions -->
|
|
|
|
| 707 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 708 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 709 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 710 |
[basic.scope]: basic.md#basic.scope
|
| 711 |
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 712 |
-
[basic.start.
|
| 713 |
[basic.start.term]: basic.md#basic.start.term
|
| 714 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 715 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 716 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 717 |
[class.copy]: special.md#class.copy
|
|
@@ -719,11 +855,10 @@ void f() {
|
|
| 719 |
[class.dtor]: special.md#class.dtor
|
| 720 |
[class.temporary]: special.md#class.temporary
|
| 721 |
[conv]: conv.md#conv
|
| 722 |
[conv.prom]: conv.md#conv.prom
|
| 723 |
[dcl.init]: dcl.md#dcl.init
|
| 724 |
-
[dcl.init.list]: dcl.md#dcl.init.list
|
| 725 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 726 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 727 |
[dcl.type]: dcl.md#dcl.type
|
| 728 |
[expr]: expr.md#expr
|
| 729 |
[expr.const]: expr.md#expr.const
|
|
@@ -746,18 +881,17 @@ void f() {
|
|
| 746 |
[stmt.select]: #stmt.select
|
| 747 |
[stmt.stmt]: #stmt.stmt
|
| 748 |
[stmt.switch]: #stmt.switch
|
| 749 |
[stmt.while]: #stmt.while
|
| 750 |
[support.start.term]: language.md#support.start.term
|
|
|
|
| 751 |
|
| 752 |
[^1]: In other words, the `else` is associated with the nearest un-elsed
|
| 753 |
`if`.
|
| 754 |
|
| 755 |
-
[^2]:
|
| 756 |
-
reinterpreted as a delimiter between *init-declarator*s in the
|
| 757 |
-
declaration of `__range`.
|
| 758 |
-
|
| 759 |
-
[^3]: The transfer from the condition of a `switch` statement to a
|
| 760 |
`case` label is considered a jump in this respect.
|
| 761 |
|
| 762 |
-
[^
|
| 763 |
-
execution of the initializer.
|
|
|
|
|
|
|
|
|
| 10 |
attribute-specifier-seqₒₚₜ selection-statement
|
| 11 |
attribute-specifier-seqₒₚₜ iteration-statement
|
| 12 |
attribute-specifier-seqₒₚₜ jump-statement
|
| 13 |
declaration-statement
|
| 14 |
attribute-specifier-seqₒₚₜ try-block
|
| 15 |
+
|
| 16 |
+
init-statement:
|
| 17 |
+
expression-statement
|
| 18 |
+
simple-declaration
|
| 19 |
+
|
| 20 |
+
condition:
|
| 21 |
+
expression
|
| 22 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator brace-or-equal-initializer
|
| 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 ([[stmt.iter]]). The *declarator*
|
| 30 |
+
shall not specify a function or an array. The *decl-specifier-seq* shall
|
| 31 |
+
not define a class or enumeration. If the `auto` *type-specifier*
|
| 32 |
+
appears in the *decl-specifier-seq*, the type of the identifier being
|
| 33 |
+
declared is deduced from the initializer as described in
|
| 34 |
+
[[dcl.spec.auto]].
|
| 35 |
+
|
| 36 |
+
A name introduced by a declaration in a *condition* (either introduced
|
| 37 |
+
by the *decl-specifier-seq* or the *declarator* of the condition) is in
|
| 38 |
+
scope from its point of declaration until the end of the substatements
|
| 39 |
+
controlled by the condition. If the name is redeclared in the outermost
|
| 40 |
+
block of a substatement controlled by the condition, the declaration
|
| 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` (Clause [[conv]]). If that
|
| 59 |
+
conversion is ill-formed, the program is ill-formed. The value of a
|
| 60 |
+
*condition* that is an initialized declaration in a `switch` statement
|
| 61 |
+
is the value of the declared variable if it has integral or enumeration
|
| 62 |
+
type, or of that variable implicitly converted to integral or
|
| 63 |
+
enumeration type otherwise. The value of a *condition* that is an
|
| 64 |
+
expression is the value of the expression, contextually converted to
|
| 65 |
+
`bool` for statements other than `switch`; if that conversion is
|
| 66 |
+
ill-formed, the program is ill-formed. The value of the condition will
|
| 67 |
+
be referred to as simply “the condition” where the usage is unambiguous.
|
| 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 |
+
|
| 73 |
+
In the *decl-specifier-seq* of a *condition*, each *decl-specifier*
|
| 74 |
+
shall be either a *type-specifier* or `constexpr`.
|
| 75 |
+
|
| 76 |
## Labeled statement <a id="stmt.label">[[stmt.label]]</a>
|
| 77 |
|
| 78 |
A statement can be labeled.
|
| 79 |
|
| 80 |
``` bnf
|
|
|
|
| 83 |
attribute-specifier-seqₒₚₜ 'case' constant-expression ':' statement
|
| 84 |
attribute-specifier-seqₒₚₜ 'default :' statement
|
| 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
|
| 90 |
+
the function in which it appears. Labels shall not be redeclared within
|
| 91 |
+
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 ([[basic.lookup.unqual]]) ignores
|
| 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 |
|
|
|
|
| 109 |
```
|
| 110 |
|
| 111 |
The expression is a discarded-value expression (Clause [[expr]]). All
|
| 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]]). — *end note*]
|
| 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 |
statement-seq:
|
| 134 |
statement
|
| 135 |
statement-seq statement
|
| 136 |
```
|
| 137 |
|
| 138 |
+
A compound statement defines a block scope ([[basic.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 |
+
'if constexprₒₚₜ (' init-statementₒₚₜ condition ')' statement
|
| 150 |
+
'if constexprₒₚₜ (' init-statementₒₚₜ condition ')' statement 'else' statement
|
| 151 |
+
'switch (' init-statementₒₚₜ condition ')' statement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
In Clause [[stmt.stmt]], the term *substatement* refers to the
|
| 160 |
+
contained *statement* or *statement*s that appear in the syntax
|
| 161 |
notation. The substatement in a *selection-statement* (each
|
| 162 |
substatement, in the `else` form of the `if` statement) implicitly
|
| 163 |
defines a block scope ([[basic.scope]]). If the substatement in a
|
| 164 |
selection-statement is a single statement and not a
|
| 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)
|
| 172 |
int i;
|
| 173 |
```
|
| 174 |
|
|
|
|
| 180 |
}
|
| 181 |
```
|
| 182 |
|
| 183 |
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 ([[stmt.select]]) yields `true` the first substatement
|
| 190 |
is executed. If the `else` part of the selection statement is present
|
|
|
|
| 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` ([[expr.const]]); this form is called a *constexpr if*
|
| 201 |
+
statement. If the value of the converted condition is `false`, the first
|
| 202 |
+
substatement is a *discarded statement*, otherwise the second
|
| 203 |
+
substatement, if present, is a discarded statement. During the
|
| 204 |
+
instantation of an enclosing templated entity (Clause [[temp]]), if the
|
| 205 |
+
condition is not value-dependent after its instantiation, the discarded
|
| 206 |
+
substatement (if any) is not instantiated.
|
| 207 |
+
|
| 208 |
+
[*Note 1*: Odr-uses ([[basic.def.odr]]) in a discarded statement do
|
| 209 |
+
not require an entity to be defined. — *end note*]
|
| 210 |
+
|
| 211 |
+
A `case` or `default` label appearing within such an `if` statement
|
| 212 |
+
shall be associated with a `switch` statement ([[stmt.switch]]) within
|
| 213 |
+
the same `if` statement. A label ([[stmt.label]]) declared in a
|
| 214 |
+
substatement of a constexpr if statement shall only be referred to by a
|
| 215 |
+
statement ([[stmt.goto]]) in the same substatement.
|
| 216 |
+
|
| 217 |
+
[*Example 1*:
|
| 218 |
+
|
| 219 |
+
``` cpp
|
| 220 |
+
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 221 |
+
// ... handle p
|
| 222 |
+
|
| 223 |
+
if constexpr (sizeof...(rs) > 0)
|
| 224 |
+
g(rs...); // never instantiated with an empty argument list
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
extern int x; // no definition of x required
|
| 228 |
+
|
| 229 |
+
int f() {
|
| 230 |
+
if constexpr (true)
|
| 231 |
+
return 0;
|
| 232 |
+
else if (x)
|
| 233 |
+
return x;
|
| 234 |
+
else
|
| 235 |
+
return -x;
|
| 236 |
+
}
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
— *end example*]
|
| 240 |
+
|
| 241 |
+
An `if` statement of the form
|
| 242 |
+
|
| 243 |
+
``` bnf
|
| 244 |
+
'if constexprₒₚₜ (' init-statement condition ')' statement
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
is equivalent to
|
| 248 |
+
|
| 249 |
+
and an `if` statement of the form
|
| 250 |
+
|
| 251 |
+
``` bnf
|
| 252 |
+
'if constexprₒₚₜ (' init-statement condition ')' statement 'else' statement
|
| 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 |
|
|
|
|
| 298 |
statement labeled by the default label. If no case matches and if there
|
| 299 |
is no `default` then none of the statements in the switch is executed.
|
| 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`, [[stmt.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 |
+
'switch (' init-statement condition ')' statement
|
| 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 |
'while (' condition ')' statement
|
| 329 |
'do' statement 'while (' expression ') ;'
|
| 330 |
+
'for (' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 331 |
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 332 |
```
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
``` bnf
|
| 335 |
for-range-declaration:
|
| 336 |
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator
|
| 337 |
+
attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']'
|
| 338 |
```
|
| 339 |
|
| 340 |
``` bnf
|
| 341 |
for-range-initializer:
|
| 342 |
+
expr-or-braced-init-list
|
|
|
|
| 343 |
```
|
| 344 |
|
| 345 |
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 ([[basic.scope]]) which is entered and exited each time through
|
| 352 |
the loop.
|
| 353 |
|
| 354 |
If the substatement in an iteration-statement is a single statement and
|
| 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)
|
| 362 |
int i;
|
| 363 |
```
|
| 364 |
|
|
|
|
| 370 |
}
|
| 371 |
```
|
| 372 |
|
| 373 |
Thus after the `while` statement, `i` is no longer in scope.
|
| 374 |
|
| 375 |
+
— *end example*]
|
| 376 |
+
|
| 377 |
+
If a name introduced in an *init-statement* or *for-range-declaration*
|
| 378 |
+
is redeclared in the outermost block of the substatement, the program is
|
| 379 |
+
ill-formed.
|
| 380 |
+
|
| 381 |
+
[*Example 2*:
|
| 382 |
+
|
| 383 |
+
``` cpp
|
| 384 |
+
void f() {
|
| 385 |
+
for (int i = 0; i < 10; ++i)
|
| 386 |
+
int i = 0; // error: redeclaration
|
| 387 |
+
for (int i : { 1, 2, 3 })
|
| 388 |
+
int i = 1; // error: redeclaration
|
| 389 |
+
}
|
| 390 |
+
```
|
| 391 |
+
|
| 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 ([[stmt.select]]) becomes `false`. The test
|
|
|
|
| 420 |
```
|
| 421 |
|
| 422 |
The variable created in a condition is destroyed and created with each
|
| 423 |
iteration of the loop.
|
| 424 |
|
| 425 |
+
[*Example 1*:
|
| 426 |
+
|
| 427 |
``` cpp
|
| 428 |
struct A {
|
| 429 |
int val;
|
| 430 |
A(int i) : val(i) { }
|
| 431 |
~A() { }
|
|
|
|
| 440 |
|
| 441 |
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` (Clause [[conv]]);
|
| 450 |
if that conversion is ill-formed, the program is ill-formed.
|
| 451 |
|
|
|
|
| 456 |
### The `for` statement <a id="stmt.for">[[stmt.for]]</a>
|
| 457 |
|
| 458 |
The `for` statement
|
| 459 |
|
| 460 |
``` bnf
|
| 461 |
+
'for (' init-statement conditionₒₚₜ ';' expressionₒₚₜ ')' statement
|
| 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 ([[stmt.select]]) specifies a test, sequenced
|
| 473 |
+
before each iteration, such that the loop is exited when the condition
|
| 474 |
+
becomes `false`; the expression often specifies incrementing that is
|
| 475 |
+
sequenced after each iteration. — *end note*]
|
| 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 |
|
| 481 |
+
If the *init-statement* is a declaration, the scope of the name(s)
|
| 482 |
declared extends to the end of the `for` statement.
|
| 483 |
|
| 484 |
+
[*Example 1*:
|
| 485 |
+
|
| 486 |
``` cpp
|
| 487 |
int i = 42;
|
| 488 |
int a[10];
|
| 489 |
|
| 490 |
for (int i = 0; i < 10; i++)
|
| 491 |
a[i] = i;
|
| 492 |
|
| 493 |
int j = i; // j = 42
|
| 494 |
```
|
| 495 |
|
| 496 |
+
— *end example*]
|
| 497 |
+
|
| 498 |
### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
|
| 499 |
|
| 500 |
+
The range-based `for` statement
|
| 501 |
|
| 502 |
``` bnf
|
| 503 |
+
'for (' for-range-declaration ':' for-range-initializer ')' statement
|
| 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 |
+
- `__range`, `__begin`, and `__end` are variables defined for exposition
|
| 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 `__range` and `__range + __bound`,
|
| 518 |
+
respectively, where `__bound` is the array bound. If `R` is an array
|
| 519 |
+
of unknown bound or an array of incomplete type, the program is
|
| 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]]), and if either (or both) finds at least
|
| 525 |
+
one declaration, *begin-expr* and *end-expr* are `__range.begin()`
|
| 526 |
+
and `__range.end()`, respectively;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
- otherwise, *begin-expr* and *end-expr* are `begin(__range)` and
|
| 528 |
+
`end(__range)`, respectively, where `begin` and `end` are looked up
|
| 529 |
+
in the associated namespaces ([[basic.lookup.argdep]]).
|
| 530 |
+
\[*Note 1*: Ordinary unqualified lookup ([[basic.lookup.unqual]])
|
| 531 |
+
is not performed. — *end note*]
|
| 532 |
+
|
| 533 |
+
[*Example 1*:
|
| 534 |
|
| 535 |
``` cpp
|
| 536 |
int array[5] = { 1, 2, 3, 4, 5 };
|
| 537 |
for (int& x : array)
|
| 538 |
x *= 2;
|
| 539 |
```
|
| 540 |
|
| 541 |
+
— *end example*]
|
| 542 |
+
|
| 543 |
In the *decl-specifier-seq* of a *for-range-declaration*, each
|
| 544 |
*decl-specifier* shall be either a *type-specifier* or `constexpr`. The
|
| 545 |
*decl-specifier-seq* shall not define a class or enumeration.
|
| 546 |
|
| 547 |
## Jump statements <a id="stmt.jump">[[stmt.jump]]</a>
|
|
|
|
| 550 |
|
| 551 |
``` bnf
|
| 552 |
jump-statement:
|
| 553 |
'break ;'
|
| 554 |
'continue ;'
|
| 555 |
+
'return' expr-or-braced-init-listₒₚₜ ';'
|
|
|
|
| 556 |
'goto' identifier ';'
|
| 557 |
```
|
| 558 |
|
| 559 |
On exit from a scope (however accomplished), objects with automatic
|
| 560 |
storage duration ([[basic.stc.auto]]) that have been constructed in
|
| 561 |
+
that scope are destroyed in the reverse order of their construction.
|
| 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()` ([[support.start.term]]), for example)
|
| 573 |
+
without destroying class objects with automatic storage
|
| 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
|
|
|
|
| 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 |
[[class.ctor]]), or a destructor ([[class.dtor]]). A return statement
|
| 628 |
+
with an operand of type `void` shall be used only in a function whose
|
| 629 |
+
return type is cv `void`. A return statement with any other operand
|
| 630 |
+
shall be used only in a function whose return type is not cv `void`; the
|
| 631 |
+
return statement initializes the glvalue result or prvalue result object
|
| 632 |
+
of the (explicit or implicit) function call by copy-initialization (
|
| 633 |
+
[[dcl.init]]) from the operand.
|
| 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 ([[class.copy]]). — *end note*]
|
| 641 |
+
|
| 642 |
+
[*Example 1*:
|
| 643 |
|
| 644 |
``` cpp
|
| 645 |
std::pair<std::string,int> f(const char* p, int x) {
|
| 646 |
return {p,x};
|
| 647 |
}
|
| 648 |
```
|
| 649 |
|
| 650 |
+
— *end example*]
|
|
|
|
| 651 |
|
| 652 |
+
Flowing off the end of a constructor, a destructor, or a function with a
|
| 653 |
+
cv `void` return type is equivalent to a `return` with no operand.
|
| 654 |
+
Otherwise, flowing off the end of a function other than `main` (
|
| 655 |
+
[[basic.start.main]]) results in undefined behavior.
|
| 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 ([[stmt.jump]]) of
|
| 661 |
+
the block enclosing the return statement.
|
| 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 (
|
|
|
|
| 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 ([[stmt.jump]]).
|
| 687 |
|
| 688 |
It is possible to transfer into a block, but not in a way that bypasses
|
| 689 |
+
declarations with initialization. A program that jumps[^2] from a point
|
| 690 |
where a variable with automatic storage duration is not in scope to a
|
| 691 |
point where it is in scope is ill-formed unless the variable has scalar
|
| 692 |
type, class type with a trivial default constructor and a trivial
|
| 693 |
destructor, a cv-qualified version of one of these types, or an array of
|
| 694 |
one of the preceding types and is declared without an *initializer* (
|
| 695 |
[[dcl.init]]).
|
| 696 |
|
| 697 |
+
[*Example 1*:
|
| 698 |
+
|
| 699 |
``` cpp
|
| 700 |
void f() {
|
| 701 |
// ...
|
| 702 |
goto lx; // ill-formed: jump into scope of a
|
| 703 |
// ...
|
| 704 |
ly:
|
| 705 |
X a = 1;
|
| 706 |
// ...
|
| 707 |
lx:
|
| 708 |
+
goto ly; // OK, jump implies destructor call for a followed by
|
| 709 |
+
// construction again immediately following label ly
|
|
|
|
| 710 |
}
|
| 711 |
```
|
| 712 |
|
| 713 |
+
— *end example*]
|
| 714 |
+
|
| 715 |
+
Dynamic initialization of a block-scope variable with static storage
|
| 716 |
+
duration ([[basic.stc.static]]) or thread storage duration (
|
| 717 |
+
[[basic.stc.thread]]) is performed the first time control passes through
|
| 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
|
| 723 |
+
initialized, the concurrent execution shall wait for completion of the
|
| 724 |
+
initialization.[^3] If control re-enters the declaration recursively
|
| 725 |
+
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); // recursive call - undefined
|
| 732 |
return i+1;
|
| 733 |
}
|
| 734 |
```
|
| 735 |
|
| 736 |
+
— *end example*]
|
| 737 |
+
|
| 738 |
The destructor for a block-scope object with static or thread storage
|
| 739 |
duration will be executed if and only if it was constructed.
|
| 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 ([[expr.type.conv]]) as its leftmost
|
| 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*:
|
| 755 |
+
|
| 756 |
If the *statement* cannot syntactically be a *declaration*, there is no
|
| 757 |
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* ([[dcl.type]]),
|
| 764 |
|
| 765 |
``` cpp
|
| 766 |
T(a)->m = 7; // expression-statement
|
| 767 |
T(a)++; // expression-statement
|
| 768 |
T(a,5)<<c; // expression-statement
|
|
|
|
| 775 |
|
| 776 |
In the last example above, `g`, which is a pointer to `T`, is
|
| 777 |
initialized to `double(3)`. This is of course ill-formed for semantic
|
| 778 |
reasons, but that does not affect the syntactic analysis.
|
| 779 |
|
| 780 |
+
— *end example*]
|
| 781 |
+
|
| 782 |
The remaining cases are *declaration*s.
|
| 783 |
|
| 784 |
+
[*Example 2*:
|
| 785 |
+
|
| 786 |
``` cpp
|
| 787 |
class T {
|
| 788 |
// ...
|
| 789 |
public:
|
| 790 |
T();
|
|
|
|
| 797 |
T(d),e,f=3; // declaration
|
| 798 |
extern int h;
|
| 799 |
T(g)(h,2); // declaration
|
| 800 |
```
|
| 801 |
|
| 802 |
+
— *end example*]
|
| 803 |
+
|
| 804 |
+
— *end note*]
|
| 805 |
+
|
| 806 |
The disambiguation is purely syntactic; that is, the meaning of the
|
| 807 |
names occurring in such a statement, beyond whether they are
|
| 808 |
*type-name*s or not, is not generally used in or changed by the
|
| 809 |
disambiguation. Class templates are instantiated as necessary to
|
| 810 |
determine if a qualified name is a *type-name*. Disambiguation precedes
|
| 811 |
parsing, and a statement disambiguated as a declaration may be an
|
| 812 |
ill-formed declaration. If, during parsing, a name in a template
|
| 813 |
parameter is bound differently than it would be bound during a trial
|
| 814 |
+
parse, the program is ill-formed. No diagnostic is required.
|
| 815 |
+
|
| 816 |
+
[*Note 2*: This can occur only when the name is declared earlier in the
|
| 817 |
+
declaration. — *end note*]
|
| 818 |
+
|
| 819 |
+
[*Example 3*:
|
| 820 |
|
| 821 |
``` cpp
|
| 822 |
struct T1 {
|
| 823 |
T1 operator()(int x) { return T1(x); }
|
| 824 |
int operator=(int x) { return x; }
|
|
|
|
| 828 |
int a, (*(*b)(T2))(int), c, d;
|
| 829 |
|
| 830 |
void f() {
|
| 831 |
// disambiguation requires this to be parsed as a declaration:
|
| 832 |
T1(a) = 3,
|
| 833 |
+
T2(4), // T2 will be declared as a variable of type T1, but this will not
|
| 834 |
+
(*(*b)(T2(c)))(int(d)); // allow the last part of the declaration to parse properly,
|
| 835 |
+
// since it depends on T2 being a type-name
|
|
|
|
|
|
|
|
|
|
|
|
|
| 836 |
}
|
| 837 |
```
|
| 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]: special.md#class.copy
|
|
|
|
| 855 |
[class.dtor]: special.md#class.dtor
|
| 856 |
[class.temporary]: special.md#class.temporary
|
| 857 |
[conv]: conv.md#conv
|
| 858 |
[conv.prom]: conv.md#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 |
[expr]: expr.md#expr
|
| 864 |
[expr.const]: expr.md#expr.const
|
|
|
|
| 881 |
[stmt.select]: #stmt.select
|
| 882 |
[stmt.stmt]: #stmt.stmt
|
| 883 |
[stmt.switch]: #stmt.switch
|
| 884 |
[stmt.while]: #stmt.while
|
| 885 |
[support.start.term]: language.md#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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 892 |
`case` label is considered a jump in this respect.
|
| 893 |
|
| 894 |
+
[^3]: The implementation must not introduce any deadlock around
|
| 895 |
+
execution of the initializer. Deadlocks might still be caused by the
|
| 896 |
+
program logic; the implementation need only avoid deadlocks due to
|
| 897 |
+
its own synchronization operations.
|