tmp/tmp0ww9r5sf/{from.md → to.md}
RENAMED
|
@@ -2,28 +2,26 @@
|
|
| 2 |
|
| 3 |
Selection statements choose one of several flows of control.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
selection-statement:
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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)
|
|
@@ -42,37 +40,37 @@ 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
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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`
|
| 59 |
-
|
| 60 |
substatement is a *discarded statement*, otherwise the second
|
| 61 |
substatement, if present, is a discarded statement. During the
|
| 62 |
-
|
| 63 |
condition is not value-dependent after its instantiation, the discarded
|
| 64 |
substatement (if any) is not instantiated.
|
| 65 |
|
| 66 |
-
[*Note 1*: Odr-uses
|
| 67 |
-
|
| 68 |
|
| 69 |
A `case` or `default` label appearing within such an `if` statement
|
| 70 |
-
shall be associated with a `switch` statement
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
[*Example 1*:
|
| 76 |
|
| 77 |
``` cpp
|
| 78 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
|
@@ -97,47 +95,61 @@ int f() {
|
|
| 97 |
— *end example*]
|
| 98 |
|
| 99 |
An `if` statement of the form
|
| 100 |
|
| 101 |
``` bnf
|
| 102 |
-
|
| 103 |
```
|
| 104 |
|
| 105 |
is equivalent to
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
and an `if` statement of the form
|
| 108 |
|
| 109 |
``` bnf
|
| 110 |
-
|
| 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 |
|
| 123 |
The condition shall be of integral type, enumeration type, or class
|
| 124 |
type. If of class type, the condition is contextually implicitly
|
| 125 |
-
converted
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
``` bnf
|
| 132 |
-
|
| 133 |
```
|
| 134 |
|
| 135 |
-
where the *constant-expression* shall be a converted constant
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
There shall be at most one label of the form
|
| 141 |
|
| 142 |
``` cpp
|
| 143 |
default :
|
|
@@ -146,34 +158,41 @@ default :
|
|
| 146 |
within a `switch` statement.
|
| 147 |
|
| 148 |
Switch statements can be nested; a `case` or `default` label is
|
| 149 |
associated with the smallest switch enclosing it.
|
| 150 |
|
| 151 |
-
When the `switch` statement is executed, its condition is evaluated
|
| 152 |
-
|
| 153 |
-
to the
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 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`,
|
| 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 |
-
|
| 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 |
|
|
|
|
| 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 |
+
The substatement in a *selection-statement* (each substatement, in the
|
| 18 |
+
`else` form of the `if` statement) implicitly defines a block scope
|
| 19 |
+
[[basic.scope]]. If the substatement in a *selection-statement* is a
|
| 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)
|
|
|
|
| 40 |
|
| 41 |
— *end example*]
|
| 42 |
|
| 43 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 44 |
|
| 45 |
+
If the condition [[stmt.select]] yields `true` the first substatement is
|
| 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 shall be a contextually converted constant expression of type
|
| 56 |
+
`bool` [[expr.const]]; this form is called a *constexpr if* statement.
|
| 57 |
+
If the value of the converted condition is `false`, the first
|
| 58 |
substatement is a *discarded statement*, otherwise the second
|
| 59 |
substatement, if present, is a discarded statement. During the
|
| 60 |
+
instantiation of an enclosing templated entity [[temp.pre]], if the
|
| 61 |
condition is not value-dependent after its instantiation, the discarded
|
| 62 |
substatement (if any) is not instantiated.
|
| 63 |
|
| 64 |
+
[*Note 1*: Odr-uses [[basic.def.odr]] in a discarded statement do not
|
| 65 |
+
require an entity to be defined. — *end note*]
|
| 66 |
|
| 67 |
A `case` or `default` label appearing within such an `if` statement
|
| 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) {
|
|
|
|
| 95 |
— *end example*]
|
| 96 |
|
| 97 |
An `if` statement of the form
|
| 98 |
|
| 99 |
``` bnf
|
| 100 |
+
if constexprₒₚₜ '(' init-statement condition ')' statement
|
| 101 |
```
|
| 102 |
|
| 103 |
is equivalent to
|
| 104 |
|
| 105 |
+
``` bnf
|
| 106 |
+
'{'
|
| 107 |
+
init-statement
|
| 108 |
+
if constexprₒₚₜ '(' condition ')' statement
|
| 109 |
+
'}'
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
and an `if` statement of the form
|
| 113 |
|
| 114 |
``` bnf
|
| 115 |
+
if constexprₒₚₜ '(' init-statement condition ')' statement else statement
|
| 116 |
```
|
| 117 |
|
| 118 |
is equivalent to
|
| 119 |
|
| 120 |
+
``` bnf
|
| 121 |
+
'{'
|
| 122 |
+
init-statement
|
| 123 |
+
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 124 |
+
'}'
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
except that names declared in the *init-statement* are in the same
|
| 128 |
declarative region as those declared in the *condition*.
|
| 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 condition shall be of integral type, enumeration type, or class
|
| 136 |
type. If of class type, the condition is contextually implicitly
|
| 137 |
+
converted [[conv]] to an integral or enumeration type. If the (possibly
|
| 138 |
+
converted) type is subject to integral promotions [[conv.prom]], the
|
| 139 |
+
condition is converted to the promoted type. Any statement within the
|
| 140 |
+
`switch` statement can be labeled with one or more case labels as
|
| 141 |
+
follows:
|
| 142 |
|
| 143 |
``` bnf
|
| 144 |
+
case constant-expression ':'
|
| 145 |
```
|
| 146 |
|
| 147 |
+
where the *constant-expression* shall be a converted constant expression
|
| 148 |
+
[[expr.const]] of the adjusted type of the switch condition. No two of
|
| 149 |
+
the case constants in the same switch shall have the same value after
|
| 150 |
+
conversion.
|
| 151 |
|
| 152 |
There shall be at most one label of the form
|
| 153 |
|
| 154 |
``` cpp
|
| 155 |
default :
|
|
|
|
| 158 |
within a `switch` statement.
|
| 159 |
|
| 160 |
Switch statements can be nested; a `case` or `default` label is
|
| 161 |
associated with the smallest switch enclosing it.
|
| 162 |
|
| 163 |
+
When the `switch` statement is executed, its condition is evaluated. If
|
| 164 |
+
one of the case constants has the same value as the condition, control
|
| 165 |
+
is passed to the statement following the matched case label. If no case
|
| 166 |
+
constant matches the condition, and if there is a `default` label,
|
| 167 |
+
control passes to the statement labeled by the default label. If no case
|
| 168 |
+
matches and if there is no `default` then none of the statements in the
|
| 169 |
+
switch is executed.
|
| 170 |
|
| 171 |
`case` and `default` labels in themselves do not alter the flow of
|
| 172 |
control, which continues unimpeded across such labels. To exit from a
|
| 173 |
+
switch, see `break`, [[stmt.break]].
|
| 174 |
|
| 175 |
[*Note 1*: Usually, the substatement that is the subject of a switch is
|
| 176 |
compound and `case` and `default` labels appear on the top-level
|
| 177 |
statements contained within the (compound) substatement, but this is not
|
| 178 |
required. Declarations can appear in the substatement of a `switch`
|
| 179 |
statement. — *end note*]
|
| 180 |
|
| 181 |
A `switch` statement of the form
|
| 182 |
|
| 183 |
``` bnf
|
| 184 |
+
switch '(' init-statement condition ')' statement
|
| 185 |
```
|
| 186 |
|
| 187 |
is equivalent to
|
| 188 |
|
| 189 |
+
``` bnf
|
| 190 |
+
'{'
|
| 191 |
+
init-statement
|
| 192 |
+
switch '(' condition ')' statement
|
| 193 |
+
'}'
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
except that names declared in the *init-statement* are in the same
|
| 197 |
declarative region as those declared in the *condition*.
|
| 198 |
|