Declaration Statements




A declaration statement introduces a new identifier into the scope of the block within which it occurs. If an identifier with the same name exists within an outer block or with global scope then that identifier is hidden. In such cases, upon exiting the block, the visibility of the hidden identifier is restored.


An automatic variable is initialized (constructed) each time its declaration statement is executed. An automatic variable is destroyed upon exiting the block containing its declaration statement. Any form of program flow control statement that causes a control transfer either out of the block containing an initialized automatic variable or to a position within the block but before the automatic variable causes the variable to be destroyed. Loops that repeatedly execute the declaration of an automatic, cause that automatic to be destroyed and reinitialized as many times as the declaration statement for the variable is encountered.

It is illegal to transfer control to a position within a program such that the initialization of an automatic within the scope of the target position is bypassed. For example, consider the following code fragment.

goto initialization_bypassed;
 int i=1;
 initialization_bypassed: i++; // illegal to jump here because initialization of i is bypassed.

Attempting to jump to the label initialization_bypassed causes a syntax error; however, it is legal to jump over a block containing an initialized automatic, as shown below.

int i=1;
goto no_problem;
 int i=2;    // conceals outer i but never gets here anyway
no_problem: i++; // legal to jump here because initialization of i hidden inside block.

Jumping within a block can cause a destructor to be called as shown below.

  string hello("Hello World");
 goto before_string; // causes the destructor for hello to be called
                     // the string is then recreated.


A static local variable is initialized only when control first passes through its declaration statement. Default initialization to zero of all statics takes place during program initialization; thus, when a local static is initialized with a non-constant expression, it is guaranteed that the default initialization of all statics has already taken place. Local statics are destroyed upon program termination.