tmp/tmpdf597ax0/{from.md → to.md}
RENAMED
|
@@ -1,37 +1,41 @@
|
|
| 1 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 2 |
|
| 3 |
-
If the condition [[stmt.
|
| 4 |
executed. If the `else` part of the selection statement is present and
|
| 5 |
the condition yields `false`, the second substatement is executed. If
|
| 6 |
the first substatement is reached via a label, the condition is not
|
| 7 |
evaluated and the second substatement is not executed. In the second
|
| 8 |
form of `if` statement (the one including `else`), if the first
|
| 9 |
substatement is also an `if` statement then that inner `if` statement
|
| 10 |
shall contain an `else` part.[^1]
|
| 11 |
|
| 12 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 13 |
-
condition
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
substatement is a *discarded statement*,
|
| 17 |
-
substatement, if present, is a discarded statement.
|
| 18 |
-
instantiation of an enclosing templated entity [[temp.pre]],
|
| 19 |
-
condition is not value-dependent after its instantiation, the
|
| 20 |
-
substatement (if any) is not instantiated.
|
| 21 |
-
|
| 22 |
-
[
|
| 23 |
-
require an entity to be defined. — *end note*]
|
| 24 |
-
|
| 25 |
-
A `case` or `default` label appearing within such an `if` statement
|
| 26 |
-
shall be associated with a `switch` statement [[stmt.switch]] within the
|
| 27 |
-
same `if` statement. A label [[stmt.label]] declared in a substatement
|
| 28 |
-
of a constexpr if statement shall only be referred to by a statement
|
| 29 |
-
[[stmt.goto]] in the same substatement.
|
| 30 |
|
| 31 |
[*Example 1*:
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
``` cpp
|
| 34 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 35 |
// ... handle p
|
| 36 |
|
| 37 |
if constexpr (sizeof...(rs) > 0)
|
|
@@ -80,8 +84,61 @@ is equivalent to
|
|
| 80 |
init-statement
|
| 81 |
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 82 |
'}'
|
| 83 |
```
|
| 84 |
|
| 85 |
-
except that
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
|
|
|
| 1 |
### The `if` statement <a id="stmt.if">[[stmt.if]]</a>
|
| 2 |
|
| 3 |
+
If the condition [[stmt.pre]] yields `true` the first substatement is
|
| 4 |
executed. If the `else` part of the selection statement is present and
|
| 5 |
the condition yields `false`, the second substatement is executed. If
|
| 6 |
the first substatement is reached via a label, the condition is not
|
| 7 |
evaluated and the second substatement is not executed. In the second
|
| 8 |
form of `if` statement (the one including `else`), if the first
|
| 9 |
substatement is also an `if` statement then that inner `if` statement
|
| 10 |
shall contain an `else` part.[^1]
|
| 11 |
|
| 12 |
If the `if` statement is of the form `if constexpr`, the value of the
|
| 13 |
+
condition is contextually converted to `bool` and the converted
|
| 14 |
+
expression shall be a constant expression [[expr.const]]; this form is
|
| 15 |
+
called a *constexpr if* statement. If the value of the converted
|
| 16 |
+
condition is `false`, the first substatement is a *discarded statement*,
|
| 17 |
+
otherwise the second substatement, if present, is a discarded statement.
|
| 18 |
+
During the instantiation of an enclosing templated entity [[temp.pre]],
|
| 19 |
+
if the condition is not value-dependent after its instantiation, the
|
| 20 |
+
discarded substatement (if any) is not instantiated. Each substatement
|
| 21 |
+
of a constexpr if statement is a control-flow-limited statement
|
| 22 |
+
[[stmt.label]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
[*Example 1*:
|
| 25 |
|
| 26 |
+
``` cpp
|
| 27 |
+
if constexpr (sizeof(int[2])) {} // OK, narrowing allowed
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
— *end example*]
|
| 31 |
+
|
| 32 |
+
[*Note 1*: Odr-uses [[term.odr.use]] in a discarded statement do not
|
| 33 |
+
require an entity to be defined. — *end note*]
|
| 34 |
+
|
| 35 |
+
[*Example 2*:
|
| 36 |
+
|
| 37 |
``` cpp
|
| 38 |
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
|
| 39 |
// ... handle p
|
| 40 |
|
| 41 |
if constexpr (sizeof...(rs) > 0)
|
|
|
|
| 84 |
init-statement
|
| 85 |
if constexprₒₚₜ '(' condition ')' statement else statement
|
| 86 |
'}'
|
| 87 |
```
|
| 88 |
|
| 89 |
+
except that the *init-statement* is in the same scope as the
|
| 90 |
+
*condition*.
|
| 91 |
+
|
| 92 |
+
An `if` statement of the form `if consteval` is called a
|
| 93 |
+
*consteval if statement*. The *statement*, if any, in a consteval if
|
| 94 |
+
statement shall be a *compound-statement*.
|
| 95 |
+
|
| 96 |
+
[*Example 3*:
|
| 97 |
+
|
| 98 |
+
``` cpp
|
| 99 |
+
constexpr void f(bool b) {
|
| 100 |
+
if (true)
|
| 101 |
+
if consteval { }
|
| 102 |
+
else ; // error: not a compound-statement; else not associated with outer if
|
| 103 |
+
}
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
— *end example*]
|
| 107 |
+
|
| 108 |
+
If a consteval if statement is evaluated in a context that is manifestly
|
| 109 |
+
constant-evaluated [[expr.const]], the first substatement is executed.
|
| 110 |
+
|
| 111 |
+
[*Note 2*: The first substatement is an immediate function
|
| 112 |
+
context. — *end note*]
|
| 113 |
+
|
| 114 |
+
Otherwise, if the `else` part of the selection statement is present,
|
| 115 |
+
then the second substatement is executed. Each substatement of a
|
| 116 |
+
consteval if statement is a control-flow-limited statement
|
| 117 |
+
[[stmt.label]].
|
| 118 |
+
|
| 119 |
+
An `if` statement of the form
|
| 120 |
+
|
| 121 |
+
``` bnf
|
| 122 |
+
if '!' consteval compound-statement
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
is not itself a consteval if statement, but is equivalent to the
|
| 126 |
+
consteval if statement
|
| 127 |
+
|
| 128 |
+
``` bnf
|
| 129 |
+
if consteval '{' '}' else compound-statement
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
An `if` statement of the form
|
| 133 |
+
|
| 134 |
+
``` bnf
|
| 135 |
+
if '!' consteval compound-statement₁ else statement₂
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
is not itself a consteval if statement, but is equivalent to the
|
| 139 |
+
consteval if statement
|
| 140 |
+
|
| 141 |
+
``` bnf
|
| 142 |
+
if consteval statement₂ else compound-statement₁
|
| 143 |
+
```
|
| 144 |
|