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
. [
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]
A
case or
default label appearing within such an
if statement shall be associated with a
switch
statement (
[stmt.switch]) within the same
if statement
. A label (
[stmt.label]) declared in a substatement of a constexpr if
statement shall only be referred to by a statement (
[stmt.goto]) in
the same substatement
. [
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]