You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An if-else statement controls conditional branching. Statements in the *`if-branch`* are executed only if the *`condition`* evaluates to a non-zero value (or **`true`**). If the value of *`condition`* is nonzero, the following statement gets executed, and the statement following the optional **`else`** gets skipped. Otherwise, the following statement gets skipped, and if there's an **`else`** then the statement following the **`else`** gets executed.
10
+
An if-else statement controls conditional branching. Statements in the *`if-branch`* are executed only if the *`condition`* evaluates to a nonzero value (or **`true`**). If the value of *`condition`* is nonzero, the following statement gets executed, and the statement following the optional **`else`** gets skipped. Otherwise, the following statement gets skipped, and if there's an **`else`** then the statement following the **`else`** gets executed.
12
11
13
-
*`condition`* expressions that evaluate to non-zero are:
12
+
*`condition`* expressions that evaluate to nonzero are:
14
13
15
14
-**`true`**
16
15
- a non-null pointer,
17
-
- any non-zero arithmetic value, or
16
+
- any nonzero arithmetic value, or
18
17
- a class type that defines an unambiguous conversion to an arithmetic, boolean, or pointer type. (For information about conversions, see [Standard Conversions](../cpp/standard-conversions.md).)
19
18
20
19
## Syntax
@@ -69,52 +68,54 @@ This sample code shows several **`if`** statements in use, both with and without
69
68
70
69
usingnamespacestd;
71
70
72
-
classC
73
-
{
74
-
public:
75
-
void do_something(){}
76
-
};
77
-
voidinit(C){}
78
-
bool is_true() { return true; }
79
-
int x = 10;
80
-
81
71
intmain()
82
72
{
83
-
if (is_true())
73
+
int x = 10;
74
+
75
+
if (x < 11)
84
76
{
85
-
cout << "b is true!\n"; // executed
77
+
cout << "x < 11 is true!\n"; // executed
86
78
}
87
79
else
88
80
{
89
-
cout << "b is false!\n";
81
+
cout << "x < 11 is false!\n"; // not executed
90
82
}
91
83
92
84
// no else statement
93
-
if (x == 10)
85
+
bool flag = false;
86
+
if (flag == true)
94
87
{
95
-
x = 0;
88
+
x = 100; // not executed
96
89
}
97
90
98
-
C* c;
99
-
init(c);
100
-
if (c)
91
+
int *p = new int(25);
92
+
if (p)
101
93
{
102
-
c->do_something();
94
+
cout << *p << "\n"; // outputs 25
103
95
}
104
96
else
105
97
{
106
-
cout << "c is null!\n";
98
+
cout << "p is null!\n"; // executed if memory allocation fails
107
99
}
108
100
}
109
101
```
110
102
103
+
Output:
104
+
105
+
```output
106
+
x < 11 is true!
107
+
25
108
+
```
109
+
111
110
## <aname="if_with_init"></a> if statement with an initializer
112
111
113
-
Starting in C++17, an **`if`** statement may also contain an *`init-statement`* expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-statement. **Microsoft-specific**: This form is available starting in Visual Studio 2017 version 15.3, and requires at least the [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) compiler option.
112
+
Starting in C++17, an **`if`** statement might also contain an *`init-statement`* expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-statement. **Microsoft-specific**: This form is available starting in Visual Studio 2017 version 15.3, and requires at least the [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) compiler option.
114
113
115
114
### Example
116
115
117
116
```cpp
117
+
// Compile with /std:c++17
118
+
118
119
#include<iostream>
119
120
#include<mutex>
120
121
#include<map>
@@ -123,28 +124,27 @@ Starting in C++17, an **`if`** statement may also contain an *`init-statement`*
Starting in C++17, you can use an **`if constexpr`** statement in function templates to make compile-time branching decisions without having to resort to multiple function overloads. **Microsoft-specific**: This form is available starting in Visual Studio 2017 version 15.3, and requires at least the [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) compiler option.
162
171
163
172
### Example
164
173
165
-
This example shows how you can write a single function that handles parameter unpacking. No zero-parameter overload is needed:
174
+
This example shows how you can conditionally compile a template based on the type sent to it:
166
175
167
176
```cpp
168
-
template <classT, class... Rest>
169
-
void f(T&& t, Rest&&... r)
170
-
{
171
-
// handle t
172
-
do_something(t);
177
+
// Compile with /std:c++17
178
+
#include<iostream>
173
179
174
-
// handle r conditionally
175
-
if constexpr (sizeof...(r))
180
+
template<typename T>
181
+
autoShow(T t)
182
+
{
183
+
//if (std::is_pointer_v<T>) // Show(a) results in compiler error for return *t. Show(b) results in compiler error for return t.
184
+
if constexpr (std::is_pointer_v<T>) // This statement goes away for Show(a)
176
185
{
177
-
f(r...);
186
+
return *t;
178
187
}
179
188
else
180
189
{
181
-
g(r...);
190
+
return t;
182
191
}
183
192
}
193
+
194
+
int main()
195
+
{
196
+
int a = 42;
197
+
int* pB = &a;
198
+
199
+
std::cout << Show(a) << "\n"; // prints "42"
200
+
std::cout << Show(pB) << "\n"; // prints "42"
201
+
}
202
+
```
203
+
204
+
The **`if constexpr`** statement is evaluated at compile time, and the compiler only generates code for the **`if`** branch that matches the type of the argument sent to the function template. If you comment out the **`if constexpr`** statement and uncomment the **`if`** statement, the compiler generates code for both branches. That means you get an error:
205
+
- If you call `ShowValue(a);` you get an error on `return *t` because `t` isn't a pointer, even though the **`if`** statement is false and the code is never executed.
206
+
- If you call `ShowValue(pB);` you get an error on `return t` because `t` is a pointer, even though the **`if`** statement is true and the code is never executed.
207
+
208
+
Using `if constexpr` solves this problem because only the statement that matches the type of the argument sent to the function template is compiled.
0 commit comments