2.8 KiB
[basic.scope.block]
6 Basics [basic]
6.4 Scope [basic.scope]
6.4.3 Block scope [basic.scope.block]
Each
selection, iteration, or expansion statement ([stmt.select], [stmt.iter], [stmt.expand]),
substatement of such a statement,
handler ([except.pre]), or
compound statement ([stmt.block]) that is not the compound-statement of a handler
introduces a block scope that includes that statement or handler.
[Note 1:
A substatement that is also a block has only one scope.
â end note]
A variable that belongs to a block scope is a block variable.
[Example 1: int i = 42;int a[10];
for (int i = 0; i < 10; i++) a[i] = i;
int j = i; // j = 42 â end example]
If a declaration that is not a name-independent declaration and that binds a name in the block scope S of a
compound-statement of a lambda-expression,function-body, or function-try-block,
substatement of a selection or iteration statement that is not itself a selection or iteration statement, or
handler of a function-try-block
potentially conflicts with a declaration whose target scope is the parent scope of S, the program is ill-formed.
[Example 2: if (int x = f()) {int x; // error: redeclaration of x}else {int x; // error: redeclaration of x} â end example]