Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The C++17 if statement with an initializer permits the execution of an initialization statement immediately before the evaluation of the conditional expression. This construct tightly binds the lifecycle of the initialized variables to the lexical scope of the if statement and its associated else clauses, preventing namespace pollution.

Syntax

if (init-statement condition) {
    // true-block
} else {
    // false-block
}

Mechanics and Evaluation Order

  1. init-statement: Executed first. This must be either an expression-statement or a simple-declaration. Because both of these grammar constructs inherently terminate with a semicolon, the semicolon is formally part of the init-statement itself, not a separator defined by the if statement’s grammar.
  2. condition: Evaluated immediately after the init-statement. The condition can be either:
    • An expression that is contextually convertible to bool.
    • A declaration of a single non-array variable with a brace-or-equal-initializer (e.g., int y = get_value()). In this case, the value of the declared variable is contextually converted to bool.
If the condition evaluates to true, the true-block executes; otherwise, the false-block executes.

Scope and Lifetime Rules

Variables declared within the init-statement have a strictly defined lexical scope. The variable is visible and accessible in:
  • The condition.
  • The true-block.
  • The false-block (the else clause).
  • Any subsequent else if conditions and their respective blocks.
Declarative Region Constraints: The C++ standard dictates that names declared in the init-statement and the condition share the same declarative region. Consequently, a variable declared in the condition cannot shadow a variable declared in the init-statement. Attempting to do so results in a redeclaration error:
// INVALID: Redeclaration error. 'x' in the condition cannot shadow 'x' in the init-statement.
if (int x = 1; int x = 2) { } 
The variables are automatically destroyed when the execution of the entire if/else if/else chain terminates.

Code Visualization

if (int status = get_status(); status == 0) {
    // 'status' is in scope.
    // Executes if status is 0.
} else if (status == 1) {
    // 'status' remains in scope for subsequent else-if blocks.
    // Executes if status is 1.
} else {
    // 'status' remains in scope for the final else block.
    // Executes if status is neither 0 nor 1.
}
// 'status' is now out of scope. Its destructor (if applicable) has been called.

Condition as a Declaration

The condition itself can also be a declaration, working in tandem with the init-statement:
if (int x = 0; int y = get_value()) {
    // Both 'x' and 'y' are in scope.
    // Executes if 'y' evaluates to true (non-zero).
}

Structured Binding Integration

The init-statement fully supports C++17 structured bindings, allowing the unpacking of tuples, pairs, or structs directly into the conditional scope:
if (auto [x, y] = get_coordinates(); x > y) {
    // Both 'x' and 'y' are in scope and initialized.
}
Master C++ with Deep Grasping Methodology!Learn More