%!TEX root = std.tex
\rSec0[stmt.stmt]{Statements}%
\indextext{statement|(}

%gram: \rSec1[gram.stmt]{Statements}
%gram:

\indextext{block statement|see{statement, compound}}

\pnum
Except as indicated, statements are executed in sequence.

\begin{bnf}
\nontermdef{statement}\br
    labeled-statement\br
    attribute-specifier-seq\opt expression-statement\br
    attribute-specifier-seq\opt compound-statement\br
    attribute-specifier-seq\opt selection-statement\br
    attribute-specifier-seq\opt iteration-statement\br
    attribute-specifier-seq\opt jump-statement\br
    declaration-statement\br
    attribute-specifier-seq\opt try-block
\end{bnf}

The optional \grammarterm{attribute-specifier-seq} appertains to the respective statement.

\rSec1[stmt.label]{Labeled statement}%
\indextext{statement!labeled}

\pnum
\indextext{statement!labeled}%
\indextext{\idxcode{:}!label~specifier}%
A statement can be labeled.

\begin{bnf}
\nontermdef{labeled-statement}\br
    attribute-specifier-seq\opt identifier \terminal{:} statement\br
    attribute-specifier-seq\opt \terminal{case} constant-expression \terminal{:} statement\br
    attribute-specifier-seq\opt \terminal{default :} statement
\end{bnf}

The optional \grammarterm{attribute-specifier-seq} appertains to the label. An identifier label declares the identifier. The only use of an
identifier label is as the target of a
\indextext{statement!\idxcode{goto}}%
\tcode{goto}.
\indextext{label!scope~of}%
The scope of a label is the function in which it appears. Labels shall
not be redeclared within a function. A label can be used in a
\tcode{goto} statement before its definition.
\indextext{name~space!label}%
Labels have their own name space and do not interfere with other
identifiers.

\pnum
\indextext{label!\idxcode{case}}%
\indextext{label!\idxcode{default}}%
Case labels and default labels shall occur only in switch statements.


\rSec1[stmt.expr]{Expression statement}%
\indextext{statement!expression}

\pnum
Expression statements have the form

\begin{bnf}
\nontermdef{expression-statement}\br
    expression\opt \terminal{;}
\end{bnf}

The expression is
a discarded-value expression (Clause~\ref{expr}).
All
\indextext{side effects}%
side effects from an expression statement
are completed before the next statement is executed.
\indextext{statement!null}%
\indextext{statement!empty}%
An expression statement with the expression missing is called a null
statement.
\enternote
Most statements are expression statements --- usually assignments or
function calls. A null statement is useful to carry a label just before
the \tcode{\}} of a compound statement and to supply a null body to an
iteration statement such as a \tcode{while}
statement~(\ref{stmt.while}).
\exitnote

\rSec1[stmt.block]{Compound statement or block}%
\indextext{statement!compound}%
\indextext{\idxcode{\{\}}!block statement}%

\pnum
So that several statements can be used where one is expected, the
compound statement (also, and equivalently, called ``block'') is
provided.

\begin{bnf}
\nontermdef{compound-statement}\br
    \terminal{\{} statement-seq\opt \terminal{\}}
\end{bnf}

\begin{bnf}
\nontermdef{statement-seq}\br
    statement\br
    statement-seq statement
\end{bnf}

A compound statement defines a block scope~(\ref{basic.scope}).
\enternote
A declaration is a \grammarterm{}{statement}~(\ref{stmt.dcl}).
\exitnote

\rSec1[stmt.select]{Selection statements}%
\indextext{statement!selection|(}

\pnum
Selection statements choose one of several flows of control.

\indextext{statement!\idxcode{if}}%
\indextext{statement!\idxcode{switch}}%
%
\begin{bnf}
\nontermdef{selection-statement}\br
    \terminal{if (} condition \terminal{)} statement\br
    \terminal{if (} condition \terminal{)} statement \terminal{else} statement\br
    \terminal{switch (} condition \terminal{)} statement
\end{bnf}

\begin{bnf}
\nontermdef{condition}\br
    expression\br
    attribute-specifier-seq\opt decl-specifier-seq declarator \terminal{=} initializer-clause\br
    attribute-specifier-seq\opt decl-specifier-seq declarator braced-init-list
\end{bnf}

See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a condition.
In Clause~\ref{stmt.stmt}, the term \term{substatement} refers to
the contained \grammarterm{}{statement} or \grammarterm{}{statement}{s} that appear
in the syntax notation.
\indextext{scope!\idxgram{selection-statement}}%
The substatement in a \grammarterm{selection-statement} (each substatement,
in the \tcode{else} form of the \tcode{if} statement) implicitly defines
a block scope~(\ref{basic.scope}). If the substatement in a
selection-statement is a single statement and not a
\grammarterm{compound-statement,} it is as if it was rewritten to be a
compound-statement containing the original substatement.
\enterexample

\begin{codeblock}
if (x)
  int i;
\end{codeblock}

can be equivalently rewritten as

\begin{codeblock}
if (x) {
  int i;
}
\end{codeblock}

Thus after the \tcode{if} statement, \tcode{i} is no longer in scope.
\exitexample

\pnum
\indextext{\idxgram{condition}{s}!rules~for}%
The rules for \grammarterm{}{condition}{s} apply both to
\grammarterm{selection-statement}{s} and to the \tcode{for} and \tcode{while}
statements~(\ref{stmt.iter}). The \grammarterm{}{declarator} shall not
specify a function or an array. The \grammarterm{decl-specifier-seq} shall not
define a class or enumeration. If the \tcode{auto} \nonterminal{type-specifier} appears in
the \nonterminal{decl-specifier-seq},
the type of the identifier being declared is deduced from the initializer as described in~\ref{dcl.spec.auto}.

\pnum
\indextext{statement!declaration in \tcode{if}}%
\indextext{statement!declaration in \tcode{switch}}%
A name introduced by a declaration in a \grammarterm{}{condition} (either
introduced by the \grammarterm{decl-specifier-seq} or the
\grammarterm{}{declarator} of the condition) is in scope from its point of
declaration until the end of the substatements controlled by the
condition. If the name is re-declared in the outermost block of a
substatement controlled by the condition, the declaration that
re-declares the name is ill-formed.
\enterexample

\begin{codeblock}
if (int x = f()) {
  int x;            // ill-formed, redeclaration of \tcode{x}
}
else {
  int x;            // ill-formed, redeclaration of \tcode{x}
}
\end{codeblock}
\exitexample

\pnum
The value of a \grammarterm{}{condition} that is an initialized declaration
in a statement other than a \tcode{switch} statement is the value of the
declared variable
contextually converted to \tcode{bool} (Clause~\ref{conv}).
If that
conversion is ill-formed, the program is ill-formed. The value of a
\grammarterm{}{condition} that is an initialized declaration in a
\tcode{switch} statement is the value of the declared variable if it has
integral or enumeration type, or of that variable implicitly converted
to integral or enumeration type otherwise. The value of a
\grammarterm{}{condition} that is an expression is the value of the
expression, contextually converted to \tcode{bool}
for statements other
than \tcode{switch};
if that conversion is ill-formed, the program is
ill-formed. The value of the condition will be referred to as simply
``the condition'' where the usage is unambiguous.

\pnum
If a \grammarterm{}{condition} can be syntactically resolved as either an
expression or the declaration of a block-scope name, it is interpreted as a
declaration.

\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{condition}, each
\grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \tcode{constexpr}.

\rSec2[stmt.if]{The \tcode{if} statement}%
\indextext{statement!\idxcode{if}}

\pnum
If the condition~(\ref{stmt.select}) yields \tcode{true} the first
substatement is executed. If the \tcode{else} part of the selection
statement is present and the condition yields \tcode{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 \tcode{if} statement
(the one including \tcode{else}), if the first substatement is also an
\tcode{if} statement then that inner \tcode{if} statement shall contain
an \tcode{else} part.\footnote{In other words, the \tcode{else} is associated with the nearest un-elsed
\tcode{if}.}

\rSec2[stmt.switch]{The \tcode{switch} statement}%
\indextext{statement!\idxcode{switch}}

\pnum
The \tcode{switch} statement causes control to be transferred to one of
several statements depending on the value of a condition.

\pnum
The condition shall be of integral type, enumeration type, or class
type. If of class type, the
condition is contextually implicitly converted (Clause~\ref{conv}) to
an integral or enumeration type.
If the (possibly converted) type is subject to integral
promotions~(\ref{conv.prom}), the condition is converted
to the promoted type. 
Any
statement within the \tcode{switch} statement can be labeled with one or
more case labels as follows:

\begin{ncbnf}
\indextext{label!\idxcode{case}}%
\terminal{case} constant-expression \terminal{:}
\end{ncbnf}

where the \grammarterm{constant-expression} shall be
a converted constant expression~(\ref{expr.const}) 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.

\pnum
\indextext{label!\idxcode{default}}%
There shall be at most one label of the form

\begin{codeblock}
default :
\end{codeblock}

within a \tcode{switch} statement.

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

\pnum
When the \tcode{switch} statement is executed, its condition is
evaluated and compared with each case constant.
\indextext{label!\idxcode{case}}%
If one of the case constants is equal to the value of the condition,
control is passed to the statement following the matched case label. If
no case constant matches the condition, and if there is a
\indextext{label!\idxcode{default}}%
\tcode{default} label, control passes to the statement labeled by the
default label. If no case matches and if there is no \tcode{default}
then none of the statements in the switch is executed.

\pnum
\tcode{case} and \tcode{default} labels in themselves do not alter the
flow of control, which continues unimpeded across such labels. To exit
from a switch, see \tcode{break},~\ref{stmt.break}.
\enternote
Usually, the substatement that is the subject of a switch is compound
and \tcode{case} and \tcode{default} labels appear on the top-level
statements contained within the (compound) substatement, but this is not
required.
\indextext{statement!declaration~in \tcode{switch}}%
Declarations can appear in the substatement of a
\grammarterm{switch-statement}.
\exitnote%
\indextext{statement!selection|)}

\rSec1[stmt.iter]{Iteration statements}%
\indextext{statement!iteration|(}

\pnum
Iteration statements specify looping.

\indextext{statement!\idxcode{while}}%
\indextext{statement!\idxcode{do}}%
\indextext{statement!\idxcode{for}}%
%
\begin{bnf}
\nontermdef{iteration-statement}\br
    \terminal{while (} condition \terminal{)} statement\br
    \terminal{do} statement \terminal{while (} expression \terminal{) ;}\br
    \terminal{for (} for-init-statement condition\opt \terminal{;} expression\opt \terminal{)} statement\br
    \terminal{for (} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{bnf}

\begin{bnf}
\nontermdef{for-init-statement}\br
    expression-statement\br
    simple-declaration
\end{bnf}

\begin{bnf}
\nontermdef{for-range-declaration}\br
    attribute-specifier-seq\opt decl-specifier-seq declarator
\end{bnf}

\begin{bnf}
\nontermdef{for-range-initializer}\br
    expression\br
    braced-init-list
\end{bnf}

See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a
\grammarterm{for-range-declaration}.
\enternote
A \grammarterm{for-init-statement} ends with a semicolon.
\exitnote

\pnum
The substatement in an \grammarterm{iteration-statement} implicitly defines
a block scope~(\ref{basic.scope}) which is entered and exited each time
through the loop.

\indextext{scope!\idxgram{iteration-statement}}%
If the substatement in an iteration-statement is a single statement and
not a \grammarterm{compound-statement,} it is as if it was rewritten to be
a compound-statement containing the original statement.
\enterexample

\begin{codeblock}
while (--x >= 0)
  int i;
\end{codeblock}

can be equivalently rewritten as

\begin{codeblock}
while (--x >= 0) {
  int i;
}
\end{codeblock}

\pnum
Thus after the \tcode{while} statement, \tcode{i} is no longer in scope.
\exitexample

\pnum
\enternote
The requirements on \grammarterm{}{condition}{s} in iteration statements are
described in~\ref{stmt.select}.
\exitnote

\rSec2[stmt.while]{The \tcode{while} statement}%
\indextext{statement!\idxcode{while}}

\pnum
In the \tcode{while} statement the substatement is executed repeatedly
until the value of the condition~(\ref{stmt.select}) becomes
\tcode{false}. The test takes place before each execution of the
substatement.

\pnum
\indextext{statement!declaration~in \tcode{while}}%
When the condition of a \tcode{while} statement is a declaration, the scope of
the variable that is declared extends from its point of
declaration~(\ref{basic.scope.pdecl}) to the end of the \tcode{while}
\grammarterm{}{statement}. A \tcode{while} statement of the form

\begin{codeblock}
while (T t = x) @\grammarterm{statement}@
\end{codeblock}

is equivalent to

\begin{codeblock}
label:
{                   // start of condition scope
  T t = x;
  if (t) {
    @\grammarterm{statement}@
    goto label;
  }
}                   // end of condition scope
\end{codeblock}

The variable created in a condition is destroyed and created with each
iteration of the loop.
\enterexample

\begin{codeblock}
struct A {
  int val;
  A(int i) : val(i) { }
  ~A() { }
  operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
  // ...
  i = 0;
}
\end{codeblock}

In the while-loop, the constructor and destructor are each called twice,
once for the condition that succeeds and once for the condition that
fails.
\exitexample

\rSec2[stmt.do]{The \tcode{do} statement}%
\indextext{statement!\idxcode{do}}

\pnum
The expression is contextually converted to \tcode{bool} (Clause~\ref{conv});
if that conversion is ill-formed, the program is ill-formed.

\pnum
In the \tcode{do} statement the substatement is executed repeatedly
until the value of the expression becomes \tcode{false}. The test takes
place after each execution of the statement.

\rSec2[stmt.for]{The \tcode{for} statement}%
\indextext{statement!\idxcode{for}}

\pnum
The \tcode{for} statement

\begin{ncbnf}
\terminal{for (} for-init-statement condition\opt \terminal{;} expression\opt \terminal{)} statement
\end{ncbnf}

is equivalent to

\begin{ncbnftab}
\terminal{\{}\br
\>for-init-statement\br
\>\terminal{while (} condition \terminal{) \{}\br
\>\>statement\br
\>\>expression \terminal{;}\br
\>\terminal{\}}\br
\terminal{\}}
\end{ncbnftab}

except that names declared in the \grammarterm{for-init-statement} are in
the same declarative-region as those declared in the
\grammarterm{}{condition}, and except that a
\indextext{statement!\tcode{continue}~in \tcode{for}}%
\tcode{continue} in \grammarterm{}{statement} (not enclosed in another
iteration statement) will execute \grammarterm{}{expression} before
re-evaluating \grammarterm{}{condition}.
\enternote
Thus the first statement specifies initialization for the loop; the
condition~(\ref{stmt.select}) specifies a test, made before each
iteration, such that the loop is exited when the condition becomes
\tcode{false}; the expression often specifies incrementing that is done
after each iteration.
\exitnote

\pnum
Either or both of the condition and the expression can be omitted. A
missing \grammarterm{}{condition} makes the implied \tcode{while} clause
equivalent to \tcode{while(true)}.

\pnum
\indextext{statement!declaration~in \tcode{for}}%
\indextext{\idxcode{for}!scope~of declaration~in}%
If the \grammarterm{for-init-statement} is a declaration, the scope of the
name(s) declared extends to the end of the \grammarterm{for-statement}.
\enterexample

\begin{codeblock}
int i = 42;
int a[10];

for (int i = 0; i < 10; i++)
  a[i] = i;

int j = i;          // \tcode{j = 42}
\end{codeblock}
\exitexample

\rSec2[stmt.ranged]{The range-based \tcode{for} statement}%
\indextext{statement!range~based \idxcode{for}}

\pnum
For a range-based \tcode{for} statement of the form

\begin{ncbnf}
\terminal{for (} for-range-declaration : expression \terminal{)} statement
\end{ncbnf}

let \textit{range-init} be equivalent to the \grammarterm{expression} surrounded
by parentheses\footnote{this ensures that a top-level comma operator cannot be
reinterpreted as a delimiter between \grammarterm{init-declarator}{s} in the
declaration of \tcode{__range}.}

\begin{ncbnf}
\terminal{(} expression \terminal{)}
\end{ncbnf}

and for a range-based \tcode{for} statement of the form

\begin{ncbnf}
\terminal{for} \terminal{(} for-range-declaration \terminal{:} braced-init-list \terminal{)} statement
\end{ncbnf}

let \textit{range-init} be equivalent to the \grammarterm{braced-init-list}. In each case, a
range-based \tcode{for} statement
is equivalent to

\begin{codeblock}
{
  auto && __range = range-init;
  for ( auto __begin = begin-expr,
             __end = end-expr;
        __begin != __end;
        ++__begin ) {
    @\textit{for-range-declaration}@ = *__begin;
    @\textit{statement}@
  }
}
\end{codeblock}

where \tcode{__range}, \tcode{__begin}, and \tcode{__end} are variables defined for
exposition only, and \tcode{_RangeT} is the type of the
\grammarterm{}{expression}, and \textit{begin-expr} and \textit{end-expr} are
determined as follows:

\begin{itemize}
\item if \tcode{_RangeT} is an array type, \textit{begin-expr} and \textit{end-expr} are
\tcode{__range} and \tcode{__range + __bound}, respectively, where \tcode{__bound} is
the array bound. If \tcode{_RangeT} is an array of unknown size or an array of
incomplete type, the program is ill-formed;

\item if \tcode{_RangeT} is a class type, the \grammarterm{unqualified-id}{s}
\tcode{begin} and \tcode{end} are looked up in the scope of class \tcode{\mbox{_RangeT}}
as if by class member access lookup~(\ref{basic.lookup.classref}), and if either
(or both) finds at least one declaration, \grammarterm{begin-expr} and
\grammarterm{end-expr} are \tcode{__range.begin()} and \tcode{__range.end()},
respectively;

\item otherwise, \textit{begin-expr} and \textit{end-expr} are \tcode{begin(__range)}
and \tcode{end(__range)}, respectively, where \tcode{begin} and \tcode{end} are looked
up in the associated namespaces~(\ref{basic.lookup.argdep}).
\enternote Ordinary unqualified lookup~(\ref{basic.lookup.unqual}) is not
performed. \exitnote
\end{itemize}

\enterexample
\begin{codeblock}
int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
  x *= 2;
\end{codeblock}
\exitexample%
\indextext{statement!iteration|)}

\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{for-range-declaration},
each \grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \tcode{constexpr}. The \grammarterm{decl-specifier-seq} shall not define a
class or enumeration.

\rSec1[stmt.jump]{Jump statements}%
\indextext{statement!jump}

\pnum
Jump statements unconditionally transfer control.
\indextext{statement!jump}%

\indextext{statement!\idxcode{break}}%
\indextext{statement!\idxcode{continue}}%
\indextext{return statement@\tcode{return} statement|see{\tcode{return}}}%
\indextext{\idxcode{return}}%
\indextext{statement!\idxcode{goto}}%
%
\begin{bnf}
\nontermdef{jump-statement}\br
    \terminal{break ;}\br
    \terminal{continue ;}\br
    \terminal{return} expression\opt \terminal{;}\br
    \terminal{return} braced-init-list \terminal{;}\br
    \terminal{goto} identifier \terminal{;}
\end{bnf}

\pnum
\indextext{local~variable!destruction~of}%
\indextext{scope!destructor~and exit~from}%
On exit from a scope (however accomplished), objects with automatic storage
duration~(\ref{basic.stc.auto}) that have been constructed in that scope are destroyed
in the reverse order of their construction. \enternote For temporaries,
see~\ref{class.temporary}. \exitnote Transfer out of a loop, out of a block, or back
past
an initialized variable with automatic storage duration involves the
destruction of objects with automatic storage duration that are in
scope at the point transferred from but not at the point transferred to.
(See~\ref{stmt.dcl} for transfers into blocks).
\enternote
However, the program can be terminated (by calling
\indextext{\idxcode{exit}}%
\indexlibrary{\idxcode{exit}}%
\tcode{std::exit()} or
\indextext{\idxcode{abort}}%
\indexlibrary{\idxcode{abort}}%
\tcode{std::abort()}~(\ref{support.start.term}), for example) without
destroying class objects with automatic storage duration.
\exitnote

\rSec2[stmt.break]{The \tcode{break} statement}%
\indextext{statement!\idxcode{break}}

\pnum
The \tcode{break} statement shall occur only in an
\indextext{\idxgram{iteration-statement}}%
\indextext{statement!\idxcode{switch}}%
\grammarterm{iteration-statement} or a \tcode{switch} statement and causes
termination of the smallest enclosing \grammarterm{iteration-statement} or
\tcode{switch} statement; control passes to the statement following the
terminated statement, if any.

\rSec2[stmt.cont]{The \tcode{continue} statement}%
\indextext{statement!\idxcode{continue}}

\pnum
The
\tcode{continue}
statement shall occur only in an
\indextext{\idxgram{iteration-statement}}%
\grammarterm{iteration-statement}
and causes control to pass to the loop-continuation portion of the
smallest enclosing \grammarterm{iteration-statement}, that is, to the end
of the loop. More precisely, in each of the statements

\begin{minipage}{.30\hsize}
\begin{codeblock}
while (foo) {
  {
    // ...
  }
contin: ;
}
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
do {
  {
    // ...
  }
contin: ;
} while (foo);
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
for (;;) {
  {
    // ...
  }
contin: ;
}
\end{codeblock}
\end{minipage}

a \tcode{continue} not contained in an enclosed iteration statement is
equivalent to \tcode{goto} \tcode{contin}.

\rSec2[stmt.return]{The \tcode{return} statement}%
\indextext{\idxcode{return}}%
\indextext{function~return|see{\tcode{return}}}%

\pnum
A function returns to its caller by the \tcode{return} statement.

\pnum
A return statement
with neither an \grammarterm{expression} nor a \grammarterm{braced-init-list}
can be used only in functions
that do not return a value, that is, a function with the return type
\cv\ \tcode{void}, a constructor~(\ref{class.ctor}), or a
destructor~(\ref{class.dtor}).
\indextext{\idxcode{return}!constructor~and}%
\indextext{\idxcode{return}!constructor~and}%
A return statement with an expression of non-void type can be used only
in functions returning a value; the value of the expression is returned
to the caller of the function.
\indextext{conversion!return~type}%
The value of the expression is implicitly converted to the return type of the
function in which it appears. A return statement can involve the
construction and copy or move of a temporary object~(\ref{class.temporary}).
\enternote
A copy or move operation associated with a return statement may be elided or
considered as an rvalue for the purpose of overload resolution in
selecting a constructor~(\ref{class.copy}).
\exitnote A return statement with a \grammarterm{braced-init-list} initializes the object or reference to be returned from the function by copy-list-initialization~(\ref{dcl.init.list}) from the specified initializer list. \enterexample

\begin{codeblock}
std::pair<std::string,int> f(const char* p, int x) {
  return {p,x};
}
\end{codeblock}
\exitexample

Flowing off the end of a function is equivalent to a \tcode{return} with
no value; this results in undefined behavior in a value-returning
function.

\pnum
A return statement with an expression of type \tcode{void}
can be used only in functions with a return type of
\cvqual{cv} \tcode{void}; the expression is evaluated just before the function
returns to its caller.

\rSec2[stmt.goto]{The \tcode{goto} statement}%
\indextext{statement!\idxcode{goto}}

\pnum
The \tcode{goto} statement unconditionally transfers control to the
statement labeled by the identifier. The identifier shall be a
\indextext{label}%
label~(\ref{stmt.label}) located in the current function.

\rSec1[stmt.dcl]{Declaration statement}%
\indextext{statement!declaration}

\pnum
A declaration statement introduces one or more new identifiers into a
block; it has the form

\begin{bnf}
\nontermdef{declaration-statement}\br
    block-declaration
\end{bnf}

If an identifier introduced by a declaration was previously declared in
an outer block,
\indextext{declaration~hiding|see{name~hiding}}%
\indextext{name~hiding}%
\indextext{block~structure}%
the outer declaration is hidden for the remainder of the block, after
which it resumes its force.

\pnum
\indextext{block!initialization~in}%
\indextext{initialization!automatic}%
Variables with automatic storage duration~(\ref{basic.stc.auto}) are
initialized each time their \grammarterm{declaration-statement} is executed.
\indextext{local~variable!destruction~of}%
Variables with automatic storage duration declared in the block are
destroyed on exit from the block~(\ref{stmt.jump}).

\pnum
\indextext{initialization!jump~past}%
\indextext{\idxcode{goto}!initialization~and}%
It is possible to transfer into a block, but not in a way that bypasses
declarations with initialization. A program that jumps\footnote{The transfer from the condition of a \tcode{switch} statement to a
\tcode{case} label is considered a jump in this respect.}
from a point where a variable with automatic storage duration is
not in scope to a point where it is in scope is ill-formed unless the
variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an
\grammarterm{}{initializer}~(\ref{dcl.init}).
\enterexample

\begin{codeblock}
void f() {
  // ... 
  goto lx;          // ill-formed: jump into scope of \tcode{a}
  // ...
ly:
  X a = 1;
  // ...
lx:
  goto ly;          // OK, jump implies destructor
                    // call for \tcode{a} followed by construction
                    // again immediately following label \tcode{ly}
}
\end{codeblock}
\exitexample

\pnum
\indextext{initialization!automatic}%
\indextext{initialization!local \tcode{static}}%
The zero-initialization~(\ref{dcl.init}) of all block-scope
variables with
static storage duration~(\ref{basic.stc.static}) or thread storage duration~(\ref{basic.stc.thread}) is performed before any
other initialization takes place. Constant initialization~(\ref{basic.start.init}) of a
block-scope entity with static storage duration, if applicable, is performed before its block is first
entered. An implementation is permitted to perform early initialization
of other block-scope variables with static or thread storage duration under the same
conditions that an implementation is permitted to statically initialize
a variable with static or thread storage duration in namespace
scope~(\ref{basic.start.init}). Otherwise such a variable is initialized
the first time control passes through its declaration; such a variable is
considered initialized upon the completion of its initialization. If the
initialization exits by throwing an exception, the initialization is not
complete, so it will be tried again the next time control enters the
declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.\footnote{The implementation must not introduce any deadlock around execution of the initializer.} If control re-enters the declaration recursively while
the variable is being initialized, the behavior is undefined.
\enterexample

\begin{codeblock}
int foo(int i) {
  static int s = foo(2*i);      // recursive call - undefined
  return i+1;
}
\end{codeblock}
\exitexample

\pnum
\indextext{\idxcode{static}!destruction~of local}%
The destructor for a block-scope object with static or thread storage duration will be
executed if and only if it was constructed.
\enternote
\ref{basic.start.term} describes the order in which block-scope objects with
static and thread storage duration are destroyed.
\exitnote

\rSec1[stmt.ambig]{Ambiguity resolution}%
\indextext{ambiguity!declaration~versus expression}

\pnum
There is an ambiguity in the grammar involving
\grammarterm{expression-statement}{s} and \grammarterm{}{declaration}{s}: An
\grammarterm{expression-statement} with a function-style explicit type
conversion~(\ref{expr.type.conv}) as its leftmost subexpression can be
indistinguishable from a \grammarterm{}{declaration} where the first
\grammarterm{}{declarator} starts with a \tcode{(}. In those cases the
\grammarterm{}{statement} is a \grammarterm{}{declaration}. \enternote To
disambiguate, the whole \grammarterm{}{statement} might have to be examined
to determine if it is an \grammarterm{expression-statement} or a
\grammarterm{}{declaration}. This disambiguates many examples.
\enterexample
assuming \tcode{T} is a
\grammarterm{simple-type-specifier}~(\ref{dcl.type}),

\begin{codeblock}
T(a)->m = 7;        // expression-statement
T(a)++;             // expression-statement
T(a,5)<<c;          // expression-statement

T(*d)(int);         //  declaration
T(e)[5];            //  declaration
T(f) = { 1, 2 };    //  declaration
T(*g)(double(3));   //  declaration
\end{codeblock}

In the last example above, \tcode{g}, which is a pointer to \tcode{T},
is initialized to \tcode{double(3)}. This is of course ill-formed for
semantic reasons, but that does not affect the syntactic analysis.
\exitexample

\pnum
The remaining cases are \grammarterm{}{declaration}{s}.
\enterexample

\begin{codeblock}
class T {
  // ...
public:
  T();
  T(int);
  T(int, int);
};
T(a);               //  declaration
T(*b)();            //  declaration
T(c)=7;             //  declaration
T(d),e,f=3;         //  declaration
extern int h;
T(g)(h,2);          //  declaration
\end{codeblock}
\exitexample
\exitnote

\pnum
The disambiguation is purely syntactic; that is, the meaning of the
names occurring in such a statement, beyond whether they are
\grammarterm{type-name}{s} or not, is not generally used in or changed by the
disambiguation. Class templates are instantiated as necessary to
determine if a qualified name is a \grammarterm{type-name}. Disambiguation
precedes parsing, and a statement disambiguated as a declaration may be
an ill-formed declaration. If, during parsing, a name in a template
parameter is bound differently than it would be bound during a trial
parse, the program is ill-formed. No diagnostic is required.
\enternote
This can occur only when the name is declared earlier in the
declaration.
\exitnote
\enterexample

\begin{codeblock}
struct T1 {
  T1 operator()(int x) { return T1(x); }
  int operator=(int x) { return x; }
  T1(int) { }
};
struct T2 { T2(int){ } };
int a, (*(*b)(T2))(int), c, d;

void f() {
  // disambiguation requires this to be parsed as a declaration:
  T1(a) = 3,
  T2(4),                        // \tcode{T2} will be declared as
  (*(*b)(T2(c)))(int(d));       // a variable of type \tcode{T1}
                                // but this will not allow
                                // the last part of the
                                // declaration to parse
                                // properly since it depends
                                // on \tcode{T2} being a type-name
}
\end{codeblock}
\exitexample%
\indextext{statement|)}
