cpp/language/if

Conditionally executes another statement.

Used where code needs to be executed based on a run-time condition.

Syntax
@1@ if statement without an else branch @2@ if statement with an else branch @3@ consteval if statement without an else branch @4@ consteval if statement with an else branch

Explanation
If the yields  after conversion to,  is executed.

If the else part of the if statement is present and yields  after conversion to,  is executed.

In the second form of if statement (the one including else), if is also an if statement then that inner if statement must contain an else part as well (in other words, in nested if-statements, the else is associated with the closest if that doesn't have an else).

{{rrev|since=c++17|

If statements with initializer
If is used, the if statement is equivalent to

or

Except that names declared by the (if  is a declaration) and names declared by  (if condition is a declaration) are in the same scope, which is also the scope of both s.

}} {{rrev|since=c++17|

Constexpr if
The statement that begins with is known as the constexpr if statement.

In a constexpr if statement, the value of must be. If the value is, then is discarded (if present), otherwise,  is discarded.

The return statements in a discarded statement do not participate in function return type deduction:

The discarded statement can a variable that is not defined:

Outside a template, a discarded statement is fully checked. is not a substitute for the preprocessing directive:

If a constexpr if statement appears inside a, and if is not  after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated.

Note: an example where the condition remains value-dependent after instantiation is a nested template, e.g.

Note: the discarded statement cannot be ill-formed for every possible specialization:

The common workaround before the implementation of for such a catch-all statement is a type-dependent expression that is always :

Labels (, labels, and ) appearing in a substatement of a constexpr if can only be referenced (by  or ) in the same substatement.

Note: a  can be used as the init-statement of a constexpr if statement to reduce the scope of the type alias.

}} {{rrev|since=c++23|

Consteval if
The statement that begins with is known as the consteval if statement. In a consteval if statement,

both and  (if any) must be compound statements.

If is not a compound statement, it will still be treated as a part of the consteval if statement (and thus results in a compilation error):

If a consteval if statement is evaluated in a, is executed. Otherwise, is executed if it is present.

A or  label appearing within a consteval if statement shall be associated with a  statement within the same if statement. A declared in a substatement of a consteval if statement shall only be referred to by a statement in the same substatement.

If the statement begins with, the and  (if any) must both be compound statements. Such statements are not considered consteval if statements, but are equivalent to consteval if statements:
 * is equivalent to.
 * is equivalent to.

in a consteval if statement (or in the negative form) is in an, in which a call to an immediate function needs not to be a constant expression.

}}

Keywords
,, ,