tmp/tmprd2ayses/{from.md → to.md}
RENAMED
|
@@ -2,32 +2,31 @@
|
|
| 2 |
|
| 3 |
Selection statements choose one of several flows of control.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
selection-statement:
|
| 7 |
-
'if (' condition ')' statement
|
| 8 |
-
'if (' condition ')' statement 'else' statement
|
| 9 |
-
'switch (' condition ')' statement
|
| 10 |
-
```
|
| 11 |
-
|
| 12 |
-
``` bnf
|
| 13 |
-
condition:
|
| 14 |
-
expression
|
| 15 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
|
| 16 |
-
attribute-specifier-seqₒₚₜ decl-specifier-seq declarator braced-init-list
|
| 17 |
```
|
| 18 |
|
| 19 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 20 |
-
condition.
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
notation. The substatement in a *selection-statement* (each
|
| 23 |
substatement, in the `else` form of the `if` statement) implicitly
|
| 24 |
defines a block scope ([[basic.scope]]). If the substatement in a
|
| 25 |
selection-statement is a single statement and not a
|
| 26 |
-
*compound-statement
|
| 27 |
compound-statement containing the original substatement.
|
| 28 |
|
|
|
|
|
|
|
| 29 |
``` cpp
|
| 30 |
if (x)
|
| 31 |
int i;
|
| 32 |
```
|
| 33 |
|
|
@@ -39,52 +38,11 @@ if (x) {
|
|
| 39 |
}
|
| 40 |
```
|
| 41 |
|
| 42 |
Thus after the `if` statement, `i` is no longer in scope.
|
| 43 |
|
| 44 |
-
|
| 45 |
-
the `for` and `while` statements ([[stmt.iter]]). The *declarator*
|
| 46 |
-
shall not specify a function or an array. The *decl-specifier-seq* shall
|
| 47 |
-
not define a class or enumeration. If the `auto` appears in the , the
|
| 48 |
-
type of the identifier being declared is deduced from the initializer as
|
| 49 |
-
described in [[dcl.spec.auto]].
|
| 50 |
-
|
| 51 |
-
A name introduced by a declaration in a *condition* (either introduced
|
| 52 |
-
by the *decl-specifier-seq* or the *declarator* of the condition) is in
|
| 53 |
-
scope from its point of declaration until the end of the substatements
|
| 54 |
-
controlled by the condition. If the name is re-declared in the outermost
|
| 55 |
-
block of a substatement controlled by the condition, the declaration
|
| 56 |
-
that re-declares the name is ill-formed.
|
| 57 |
-
|
| 58 |
-
``` cpp
|
| 59 |
-
if (int x = f()) {
|
| 60 |
-
int x; // ill-formed, redeclaration of x
|
| 61 |
-
}
|
| 62 |
-
else {
|
| 63 |
-
int x; // ill-formed, redeclaration of x
|
| 64 |
-
}
|
| 65 |
-
```
|
| 66 |
-
|
| 67 |
-
The value of a *condition* that is an initialized declaration in a
|
| 68 |
-
statement other than a `switch` statement is the value of the declared
|
| 69 |
-
variable contextually converted to `bool` (Clause [[conv]]). If that
|
| 70 |
-
conversion is ill-formed, the program is ill-formed. The value of a
|
| 71 |
-
*condition* that is an initialized declaration in a `switch` statement
|
| 72 |
-
is the value of the declared variable if it has integral or enumeration
|
| 73 |
-
type, or of that variable implicitly converted to integral or
|
| 74 |
-
enumeration type otherwise. The value of a *condition* that is an
|
| 75 |
-
expression is the value of the expression, contextually converted to
|
| 76 |
-
`bool` for statements other than `switch`; if that conversion is
|
| 77 |
-
ill-formed, the program is ill-formed. The value of the condition will
|
| 78 |
-
be referred to as simply “the condition” where the usage is unambiguous.
|
| 79 |
-
|
| 80 |
-
If a *condition* can be syntactically resolved as either an expression
|
| 81 |
-
or the declaration of a block-scope name, it is interpreted as a
|
| 82 |
-
declaration.
|
| 83 |
-
|
| 84 |
-
In the *decl-specifier-seq* of a *condition*, each *decl-specifier*
|
| 85 |
-
shall be either a *type-specifier* or `constexpr`.
|
| 86 |
|
| 87 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 88 |
|
| 89 |
If the condition ([[stmt.select]]) yields `true` the first substatement
|
| 90 |
is executed. If the `else` part of the selection statement is present
|
|
@@ -93,10 +51,72 @@ If the first substatement is reached via a label, the condition is not
|
|
| 93 |
evaluated and the second substatement is not executed. In the second
|
| 94 |
form of `if` statement (the one including `else`), if the first
|
| 95 |
substatement is also an `if` statement then that inner `if` statement
|
| 96 |
shall contain an `else` part.[^1]
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 99 |
|
| 100 |
The `switch` statement causes control to be transferred to one of
|
| 101 |
several statements depending on the value of a condition.
|
| 102 |
|
|
@@ -136,11 +156,24 @@ condition, and if there is a `default` label, control passes to the
|
|
| 136 |
statement labeled by the default label. If no case matches and if there
|
| 137 |
is no `default` then none of the statements in the switch is executed.
|
| 138 |
|
| 139 |
`case` and `default` labels in themselves do not alter the flow of
|
| 140 |
control, which continues unimpeded across such labels. To exit from a
|
| 141 |
-
switch, see `break`, [[stmt.break]].
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
substatement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
|
|
|
| 2 |
|
| 3 |
Selection statements choose one of several flows of control.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
selection-statement:
|
| 7 |
+
'if constexprₒₚₜ (' init-statementₒₚₜ condition ')' statement
|
| 8 |
+
'if constexprₒₚₜ (' init-statementₒₚₜ condition ')' statement 'else' statement
|
| 9 |
+
'switch (' init-statementₒₚₜ condition ')' statement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
```
|
| 11 |
|
| 12 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 13 |
+
condition.
|
| 14 |
+
|
| 15 |
+
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 16 |
+
|
| 17 |
+
In Clause [[stmt.stmt]], the term *substatement* refers to the
|
| 18 |
+
contained *statement* or *statement*s that appear in the syntax
|
| 19 |
notation. The substatement in a *selection-statement* (each
|
| 20 |
substatement, in the `else` form of the `if` statement) implicitly
|
| 21 |
defines a block scope ([[basic.scope]]). If the substatement in a
|
| 22 |
selection-statement is a single statement and not a
|
| 23 |
+
*compound-statement*, it is as if it was rewritten to be a
|
| 24 |
compound-statement containing the original substatement.
|
| 25 |
|
| 26 |
+
[*Example 1*:
|
| 27 |
+
|
| 28 |
``` cpp
|
| 29 |
if (x)
|
| 30 |
int i;
|
| 31 |
```
|
| 32 |
|
|
|
|
| 38 |
}
|
| 39 |
```
|
| 40 |
|
| 41 |
Thus after the `if` statement, `i` is no longer in scope.
|
| 42 |
|
| 43 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 46 |
|
| 47 |
If the condition ([[stmt.select]]) yields `true` the first substatement
|
| 48 |
is executed. If the `else` part of the selection statement is present
|
|
|
|
| 51 |
evaluated and the second substatement is not executed. In the second
|
| 52 |
form of `if` statement (the one including `else`), if the first
|
| 53 |
substatement is also an `if` statement then that inner `if` statement
|
| 54 |
shall contain an `else` part.[^1]
|
| 55 |
|
| 56 |
+
If the `if` statement is of the form `if constexpr`, the value of the
|
| 57 |
+
condition shall be a contextually converted constant expression of type
|
| 58 |
+
`bool` ([[expr.const]]); this form is called a *constexpr if*
|
| 59 |
+
statement. If the value of the converted condition is `false`, the first
|
| 60 |
+
substatement is a *discarded statement*, otherwise the second
|
| 61 |
+
substatement, if present, is a discarded statement. During the
|
| 62 |
+
instantation of an enclosing templated entity (Clause [[temp]]), if the
|
| 63 |
+
condition is not value-dependent after its instantiation, the discarded
|
| 64 |
+
substatement (if any) is not instantiated.
|
| 65 |
+
|
| 66 |
+
[*Note 1*: Odr-uses ([[basic.def.odr]]) in a discarded statement do
|
| 67 |
+
not require an entity to be defined. — *end note*]
|
| 68 |
+
|
| 69 |
+
A `case` or `default` label appearing within such an `if` statement
|
| 70 |
+
shall be associated with a `switch` statement ([[stmt.switch]]) within
|
| 71 |
+
the same `if` statement. A label ([[stmt.label]]) declared in a
|
| 72 |
+
substatement of a constexpr if statement shall only be referred to by a
|
| 73 |
+
statement ([[stmt.goto]]) in the same substatement.
|
| 74 |
+
|
| 75 |
+
[*Example 1*:
|
| 76 |
+
|
| 77 |
+
``` cpp
|
| 78 |
+
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 79 |
+
// ... handle p
|
| 80 |
+
|
| 81 |
+
if constexpr (sizeof...(rs) > 0)
|
| 82 |
+
g(rs...); // never instantiated with an empty argument list
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
extern int x; // no definition of x required
|
| 86 |
+
|
| 87 |
+
int f() {
|
| 88 |
+
if constexpr (true)
|
| 89 |
+
return 0;
|
| 90 |
+
else if (x)
|
| 91 |
+
return x;
|
| 92 |
+
else
|
| 93 |
+
return -x;
|
| 94 |
+
}
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
— *end example*]
|
| 98 |
+
|
| 99 |
+
An `if` statement of the form
|
| 100 |
+
|
| 101 |
+
``` bnf
|
| 102 |
+
'if constexprₒₚₜ (' init-statement condition ')' statement
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
is equivalent to
|
| 106 |
+
|
| 107 |
+
and an `if` statement of the form
|
| 108 |
+
|
| 109 |
+
``` bnf
|
| 110 |
+
'if constexprₒₚₜ (' init-statement condition ')' statement 'else' statement
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
is equivalent to
|
| 114 |
+
|
| 115 |
+
except that names declared in the *init-statement* are in the same
|
| 116 |
+
declarative region as those declared in the *condition*.
|
| 117 |
+
|
| 118 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 119 |
|
| 120 |
The `switch` statement causes control to be transferred to one of
|
| 121 |
several statements depending on the value of a condition.
|
| 122 |
|
|
|
|
| 156 |
statement labeled by the default label. If no case matches and if there
|
| 157 |
is no `default` then none of the statements in the switch is executed.
|
| 158 |
|
| 159 |
`case` and `default` labels in themselves do not alter the flow of
|
| 160 |
control, which continues unimpeded across such labels. To exit from a
|
| 161 |
+
switch, see `break`, [[stmt.break]].
|
| 162 |
+
|
| 163 |
+
[*Note 1*: Usually, the substatement that is the subject of a switch is
|
| 164 |
+
compound and `case` and `default` labels appear on the top-level
|
| 165 |
+
statements contained within the (compound) substatement, but this is not
|
| 166 |
+
required. Declarations can appear in the substatement of a `switch`
|
| 167 |
+
statement. — *end note*]
|
| 168 |
+
|
| 169 |
+
A `switch` statement of the form
|
| 170 |
+
|
| 171 |
+
``` bnf
|
| 172 |
+
'switch (' init-statement condition ')' statement
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
is equivalent to
|
| 176 |
+
|
| 177 |
+
except that names declared in the *init-statement* are in the same
|
| 178 |
+
declarative region as those declared in the *condition*.
|
| 179 |
|