[stmt.if] (original) (raw)
8 Statements [stmt.stmt]
8.5 Selection statements [stmt.select]
8.5.1 The if statement [stmt.if]
If the condition ([stmt.select]) yields true the first substatement is executed.
If the else part of the selection statement is present and the condition yields false, the second substatement is executed.
If the first substatement is reached via a label, the condition is not evaluated and the second substatement is not executed.
In the second form of if statement (the one including else), if the first substatement is also anif statement then that inner if statement shall contain an else part.83
If the value of the converted condition is false, the first substatement is adiscarded 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.
[ Note
:
Odr-uses in a discarded statement do not require an entity to be defined.
— end note
]
A case or default label appearing within such anif statement shall be associated with a switchstatement within the same if statement.
A label declared in a substatement of a constexpr if statement shall only be referred to by a statement in the same substatement.
[ Example
:
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) { // ... handle p
if constexpr (sizeof...(rs) > 0) g(rs...); // never instantiated with an empty argument list }
extern int x; // no definition of x required
int f() { if constexpr (true) return 0; else if (x) return x; else return -x; }
— end example
]
In other words, the else is associated with the nearest un-elsedif.