From Jason Turner

[stmt.if]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmphu642p56/{from.md → to.md} +62 -0
tmp/tmphu642p56/{from.md → to.md} RENAMED
@@ -7,5 +7,67 @@ If 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 shall be a contextually converted constant expression of type
14
+ `bool` ([[expr.const]]); this form is called a *constexpr if*
15
+ statement. If the value of the converted condition is `false`, the first
16
+ substatement is a *discarded statement*, otherwise the second
17
+ substatement, if present, is a discarded statement. During the
18
+ instantation of an enclosing templated entity (Clause  [[temp]]), if the
19
+ condition is not value-dependent after its instantiation, the discarded
20
+ substatement (if any) is not instantiated.
21
+
22
+ [*Note 1*: Odr-uses ([[basic.def.odr]]) in a discarded statement do
23
+ not 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
27
+ the same `if` statement. A label ([[stmt.label]]) declared in a
28
+ substatement of a constexpr if statement shall only be referred to by a
29
+ statement ([[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)
38
+ g(rs...); // never instantiated with an empty argument list
39
+ }
40
+
41
+ extern int x; // no definition of x required
42
+
43
+ int f() {
44
+ if constexpr (true)
45
+ return 0;
46
+ else if (x)
47
+ return x;
48
+ else
49
+ return -x;
50
+ }
51
+ ```
52
+
53
+ — *end example*]
54
+
55
+ An `if` statement of the form
56
+
57
+ ``` bnf
58
+ 'if constexprₒₚₜ (' init-statement condition ')' statement
59
+ ```
60
+
61
+ is equivalent to
62
+
63
+ and an `if` statement of the form
64
+
65
+ ``` bnf
66
+ 'if constexprₒₚₜ (' init-statement condition ')' statement 'else' statement
67
+ ```
68
+
69
+ is equivalent to
70
+
71
+ except that names declared in the *init-statement* are in the same
72
+ declarative region as those declared in the *condition*.
73
+