Files
2025-10-25 03:02:53 +03:00

14 KiB

[stmt.select]

8 Statements [stmt]

8.5 Selection statements [stmt.select]

8.5.1 General [stmt.select.general]

1

#

Selection statements choose one of several flows of control.

selection-statement:
if constexpropt ( init-statementopt condition ) statement
if constexpropt ( init-statementopt condition ) statement else statement
if !opt consteval compound-statement
if !opt consteval compound-statement else statement
switch ( init-statementopt condition ) statement

See [dcl.meaning] for the optional attribute-specifier-seq in a condition.

[Note 1:

An init-statement ends with a semicolon.

— end note]

2

#

[Note 2:

Each selection-statement and each substatement of a selection-statement has a block scope ([basic.scope.block]).

— end note]

8.5.2 The if statement [stmt.if]

1

#

If the condition ([stmt.pre]) 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.72

2

#

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 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.

Each substatement of a constexpr if statement is a control-flow-limited statement ([stmt.label]).

[Example 1: if constexpr (sizeof(int[2])) {} // OK, narrowing allowed — 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) {// ... handle pif constexpr (sizeof...(rs) > 0) g(rs...); // never instantiated with an empty argument list}extern int x; // no definition of x requiredint f() {if constexpr (true)return 0; else if (x)return x; elsereturn -x;} — end example]

3

#

An if statement of the form

if constexpropt ( init-statement condition ) statement

is equivalent to

{
init-statement
if constexpropt ( condition ) statement
}

and an if statement of the form

if constexpropt ( init-statement condition ) statement else statement

is equivalent to

{
init-statement
if constexpropt ( condition ) statement else statement
}

except that the init-statement is in the same scope as the condition.

4

#

An if statement of the form if consteval is called a consteval if statement.

The statement, if any, in a consteval if statement shall be a compound-statement.

[Example 3: constexpr void f(bool b) {if (true)if consteval { }else ; // error: not a compound-statement; else not associated with outer if} — end example]

5

#

If a consteval if statement is evaluated in a context that is manifestly constant-evaluated ([expr.const]), the first substatement is executed.

[Note 2:

The first substatement is an immediate function context.

— end note]

Otherwise, if the else part of the selection statement is present, then the second substatement is executed.

Each substatement of a consteval if statement is a control-flow-limited statement ([stmt.label]).

6

#

An if statement of the form

if ! consteval compound-statement

is not itself a consteval if statement, but is equivalent to the consteval if statement

if consteval { } else compound-statement

An if statement of the form

if ! consteval compound-statement1 else statement2

is not itself a consteval if statement, but is equivalent to the consteval if statement

if consteval statement2 else compound-statement1

72)72)

In other words, the else is associated with the nearest un-elsedif.

8.5.3 The switch statement [stmt.switch]

1

#

The switch statement causes control to be transferred to one of several statements depending on the value of a condition.

2

#

If the condition is an expression, the value of the condition is the value of the expression; otherwise, it is the value of the decision variable.

The value of the condition shall be of integral type, enumeration type, or class type.

If of class type, the condition is contextually implicitly converted to an integral or enumeration type.

If the (possibly converted) type is subject to integral promotions, the condition is converted to the promoted type.

Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression :

where the constant-expression shall be a converted constant expression of the adjusted type of the switch condition.

No two of the case constants in the same switch shall have the same value after conversion.

3

#

There shall be at most one label of the formdefault : within a switch statement.

4

#

Switch statements can be nested; a case or default label is associated with the smallest switch enclosing it.

5

#

When the switch statement is executed, its condition is evaluated.

If one of the case constants has the same value as the condition, control is passed to the statement following the matched case label.

If no case constant matches the condition, and if there is adefault label, control passes to the statement labeled by the default label.

If no case matches and if there is no default then none of the statements in the switch is executed.

6

#

case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels.

To exit from a switch, see break, [stmt.break].

[Note 1:

Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required.

Declarations can appear in the substatement of aswitch statement.

— end note]

7

#

A switch statement of the form

switch ( init-statement condition ) statement

is equivalent to

{
init-statement
switch ( condition ) statement
}

except that the init-statement is in the same scope as the condition.