If the
if statement is of the form
if constexpr,
the value of the condition
is contextually converted to
bool and
the converted expression shall be a constant expression (
[expr.const]);
this
form is called a
constexpr if statement
. If the value of the
converted condition is
false, the first substatement is a
discarded statement, otherwise the second substatement, if
present, is a discarded statement
. During the instantiation of an
enclosing templated entity (
[temp.pre]), if the condition is
not value-dependent after its instantiation, the discarded substatement
(if any) is not instantiated
. Each substatement of a constexpr if statement is a control-flow-limited
statement (
[stmt.label])
. [
Example 1:
if constexpr (sizeof(int[2])) {}
—
end example]
[
Note 1:
Odr-uses (
[basic.def.odr]) in a discarded statement do not require
an entity to be defined
. —
end note]
[
Example 2:
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
if constexpr (sizeof...(rs) > 0)
g(rs...);
}
extern int x;
int f() {
if constexpr (true)
return 0;
else if (x)
return x;
else
return -x;
}
—
end example]