[basic.scope.block] (original) (raw)
6 Basics [basic]
6.4.3 Block scope [basic.scope.block]
Each
- (1.1)
selection or iteration statement ([stmt.select], [stmt.iter]), - (1.2)
substatement of such a statement, - (1.3)
handler ([except.pre]), or - (1.4)
compound statement ([stmt.block]) that is not the compound-statement of a handler
introduces a block scopethat 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
- (2.1)
compound-statement of a lambda-expression,function-body, or function-try-block, - (2.2)
substatement of a selection or iteration statement that is not itself a selection or iteration statement, or - (2.3)
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_]