tmp/tmp03om2mve/{from.md → to.md}
RENAMED
|
@@ -1,78 +1,64 @@
|
|
| 1 |
## Selection statements <a id="stmt.select">[[stmt.select]]</a>
|
| 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 |
-
|
| 18 |
-
|
| 19 |
-
[[basic.scope]].
|
| 20 |
-
single statement and not a *compound-statement*, it is as if it was
|
| 21 |
-
rewritten to be a *compound-statement* containing the original
|
| 22 |
-
substatement.
|
| 23 |
-
|
| 24 |
-
[*Example 1*:
|
| 25 |
-
|
| 26 |
-
``` cpp
|
| 27 |
-
if (x)
|
| 28 |
-
int i;
|
| 29 |
-
```
|
| 30 |
-
|
| 31 |
-
can be equivalently rewritten as
|
| 32 |
-
|
| 33 |
-
``` cpp
|
| 34 |
-
if (x) {
|
| 35 |
-
int i;
|
| 36 |
-
}
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
Thus after the `if` statement, `i` is no longer in scope.
|
| 40 |
-
|
| 41 |
-
— *end example*]
|
| 42 |
|
| 43 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 44 |
|
| 45 |
-
If the condition [[stmt.
|
| 46 |
executed. If the `else` part of the selection statement is present and
|
| 47 |
the condition yields `false`, the second substatement is executed. If
|
| 48 |
the first substatement is reached via a label, the condition is not
|
| 49 |
evaluated and the second substatement is not executed. In the second
|
| 50 |
form of `if` statement (the one including `else`), if the first
|
| 51 |
substatement is also an `if` statement then that inner `if` statement
|
| 52 |
shall contain an `else` part.[^1]
|
| 53 |
|
| 54 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 55 |
-
condition
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
substatement is a *discarded statement*,
|
| 59 |
-
substatement, if present, is a discarded statement.
|
| 60 |
-
instantiation of an enclosing templated entity [[temp.pre]],
|
| 61 |
-
condition is not value-dependent after its instantiation, the
|
| 62 |
-
substatement (if any) is not instantiated.
|
| 63 |
-
|
| 64 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
require an entity to be defined. — *end note*]
|
| 66 |
|
| 67 |
-
|
| 68 |
-
shall be associated with a `switch` statement [[stmt.switch]] within the
|
| 69 |
-
same `if` statement. A label [[stmt.label]] declared in a substatement
|
| 70 |
-
of a constexpr if statement shall only be referred to by a statement
|
| 71 |
-
[[stmt.goto]] in the same substatement.
|
| 72 |
-
|
| 73 |
-
[*Example 1*:
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 77 |
// ... handle p
|
| 78 |
|
|
@@ -122,25 +108,80 @@ is equivalent to
|
|
| 122 |
init-statement
|
| 123 |
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 124 |
'}'
|
| 125 |
```
|
| 126 |
|
| 127 |
-
except that
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 131 |
|
| 132 |
The `switch` statement causes control to be transferred to one of
|
| 133 |
several statements depending on the value of a condition.
|
| 134 |
|
| 135 |
-
The
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
| 142 |
|
| 143 |
``` bnf
|
| 144 |
case constant-expression ':'
|
| 145 |
```
|
| 146 |
|
|
@@ -191,8 +232,8 @@ is equivalent to
|
|
| 191 |
init-statement
|
| 192 |
switch '(' condition ')' statement
|
| 193 |
'}'
|
| 194 |
```
|
| 195 |
|
| 196 |
-
except that
|
| 197 |
-
|
| 198 |
|
|
|
|
| 1 |
## Selection statements <a id="stmt.select">[[stmt.select]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="stmt.select.general">[[stmt.select.general]]</a>
|
| 4 |
+
|
| 5 |
Selection statements choose one of several flows of control.
|
| 6 |
|
| 7 |
``` bnf
|
| 8 |
selection-statement:
|
| 9 |
if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement
|
| 10 |
if constexprₒₚₜ '(' init-statementₒₚₜ condition ')' statement else statement
|
| 11 |
+
if '!'ₒₚₜ consteval compound-statement
|
| 12 |
+
if '!'ₒₚₜ consteval compound-statement else statement
|
| 13 |
switch '(' init-statementₒₚₜ condition ')' statement
|
| 14 |
```
|
| 15 |
|
| 16 |
See [[dcl.meaning]] for the optional *attribute-specifier-seq* in a
|
| 17 |
condition.
|
| 18 |
|
| 19 |
[*Note 1*: An *init-statement* ends with a semicolon. — *end note*]
|
| 20 |
|
| 21 |
+
[*Note 2*: Each *selection-statement* and each substatement of a
|
| 22 |
+
*selection-statement* has a block scope
|
| 23 |
+
[[basic.scope.block]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 26 |
|
| 27 |
+
If the condition [[stmt.pre]] yields `true` the first substatement is
|
| 28 |
executed. If the `else` part of the selection statement is present and
|
| 29 |
the condition yields `false`, the second substatement is executed. If
|
| 30 |
the first substatement is reached via a label, the condition is not
|
| 31 |
evaluated and the second substatement is not executed. In the second
|
| 32 |
form of `if` statement (the one including `else`), if the first
|
| 33 |
substatement is also an `if` statement then that inner `if` statement
|
| 34 |
shall contain an `else` part.[^1]
|
| 35 |
|
| 36 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 37 |
+
condition is contextually converted to `bool` and the converted
|
| 38 |
+
expression shall be a constant expression [[expr.const]]; this form is
|
| 39 |
+
called a *constexpr if* statement. If the value of the converted
|
| 40 |
+
condition is `false`, the first substatement is a *discarded statement*,
|
| 41 |
+
otherwise the second substatement, if present, is a discarded statement.
|
| 42 |
+
During the instantiation of an enclosing templated entity [[temp.pre]],
|
| 43 |
+
if the condition is not value-dependent after its instantiation, the
|
| 44 |
+
discarded substatement (if any) is not instantiated. Each substatement
|
| 45 |
+
of a constexpr if statement is a control-flow-limited statement
|
| 46 |
+
[[stmt.label]].
|
| 47 |
+
|
| 48 |
+
[*Example 1*:
|
| 49 |
+
|
| 50 |
+
``` cpp
|
| 51 |
+
if constexpr (sizeof(int[2])) {} // OK, narrowing allowed
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
— *end example*]
|
| 55 |
+
|
| 56 |
+
[*Note 1*: Odr-uses [[term.odr.use]] in a discarded statement do not
|
| 57 |
require an entity to be defined. — *end note*]
|
| 58 |
|
| 59 |
+
[*Example 2*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
``` cpp
|
| 62 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 63 |
// ... handle p
|
| 64 |
|
|
|
|
| 108 |
init-statement
|
| 109 |
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 110 |
'}'
|
| 111 |
```
|
| 112 |
|
| 113 |
+
except that the *init-statement* is in the same scope as the
|
| 114 |
+
*condition*.
|
| 115 |
+
|
| 116 |
+
An `if` statement of the form `if consteval` is called a
|
| 117 |
+
*consteval if statement*. The *statement*, if any, in a consteval if
|
| 118 |
+
statement shall be a *compound-statement*.
|
| 119 |
+
|
| 120 |
+
[*Example 3*:
|
| 121 |
+
|
| 122 |
+
``` cpp
|
| 123 |
+
constexpr void f(bool b) {
|
| 124 |
+
if (true)
|
| 125 |
+
if consteval { }
|
| 126 |
+
else ; // error: not a compound-statement; else not associated with outer if
|
| 127 |
+
}
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
— *end example*]
|
| 131 |
+
|
| 132 |
+
If a consteval if statement is evaluated in a context that is manifestly
|
| 133 |
+
constant-evaluated [[expr.const]], the first substatement is executed.
|
| 134 |
+
|
| 135 |
+
[*Note 2*: The first substatement is an immediate function
|
| 136 |
+
context. — *end note*]
|
| 137 |
+
|
| 138 |
+
Otherwise, if the `else` part of the selection statement is present,
|
| 139 |
+
then the second substatement is executed. Each substatement of a
|
| 140 |
+
consteval if statement is a control-flow-limited statement
|
| 141 |
+
[[stmt.label]].
|
| 142 |
+
|
| 143 |
+
An `if` statement of the form
|
| 144 |
+
|
| 145 |
+
``` bnf
|
| 146 |
+
if '!' consteval compound-statement
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
is not itself a consteval if statement, but is equivalent to the
|
| 150 |
+
consteval if statement
|
| 151 |
+
|
| 152 |
+
``` bnf
|
| 153 |
+
if consteval '{' '}' else compound-statement
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
An `if` statement of the form
|
| 157 |
+
|
| 158 |
+
``` bnf
|
| 159 |
+
if '!' consteval compound-statement₁ else statement₂
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
is not itself a consteval if statement, but is equivalent to the
|
| 163 |
+
consteval if statement
|
| 164 |
+
|
| 165 |
+
``` bnf
|
| 166 |
+
if consteval statement₂ else compound-statement₁
|
| 167 |
+
```
|
| 168 |
|
| 169 |
### The `switch` statement <a id="stmt.switch">[[stmt.switch]]</a>
|
| 170 |
|
| 171 |
The `switch` statement causes control to be transferred to one of
|
| 172 |
several statements depending on the value of a condition.
|
| 173 |
|
| 174 |
+
The value of a *condition* that is an initialized declaration is the
|
| 175 |
+
value of the declared variable, or the value of the *expression*
|
| 176 |
+
otherwise. The value of the condition shall be of integral type,
|
| 177 |
+
enumeration type, or class type. If of class type, the condition is
|
| 178 |
+
contextually implicitly converted [[conv]] to an integral or enumeration
|
| 179 |
+
type. If the (possibly converted) type is subject to integral promotions
|
| 180 |
+
[[conv.prom]], the condition is converted to the promoted type. Any
|
| 181 |
+
statement within the `switch` statement can be labeled with one or more
|
| 182 |
+
case labels as follows:
|
| 183 |
|
| 184 |
``` bnf
|
| 185 |
case constant-expression ':'
|
| 186 |
```
|
| 187 |
|
|
|
|
| 232 |
init-statement
|
| 233 |
switch '(' condition ')' statement
|
| 234 |
'}'
|
| 235 |
```
|
| 236 |
|
| 237 |
+
except that the *init-statement* is in the same scope as the
|
| 238 |
+
*condition*.
|
| 239 |
|