Files
2025-10-25 03:02:53 +03:00

74 lines
2.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[basic.scope.block]
# 6 Basics [[basic]](./#basic)
## 6.4 Scope [[basic.scope]](basic.scope#block)
### 6.4.3 Block scope [basic.scope.block]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1391)
Each
- [(1.1)](#1.1)
selection, iteration, or expansion statement ([[stmt.select]](stmt.select "8.5Selection statements"), [[stmt.iter]](stmt.iter "8.6Iteration statements"), [[stmt.expand]](stmt.expand "8.7Expansion statements")),
- [(1.2)](#1.2)
substatement of such a statement,
- [(1.3)](#1.3)
[*handler*](except.pre#nt:handler "14.1Preamble[except.pre]") ([[except.pre]](except.pre "14.1Preamble")), or
- [(1.4)](#1.4)
compound statement ([[stmt.block]](stmt.block "8.4Compound statement or block"))
that is not the [*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]") of a [*handler*](except.pre#nt:handler "14.1Preamble[except.pre]")
introduces a [*block scope*](#def:scope,block "6.4.3Block scope[basic.scope.block]") that includes that statement or [*handler*](except.pre#nt:handler "14.1Preamble[except.pre]")[.](#1.sentence-1)
[*Note [1](#note-1)*:
A substatement that is also a block has only one scope[.](#1.sentence-2)
— *end note*]
A variable that belongs to a block scope is a [*block variable*](#def:block_variable "6.4.3Block scope[basic.scope.block]")[.](#1.sentence-3)
[*Example [1](#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*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L1423)
If a declaration
that is not a name-independent declaration and
that binds a name in the block scope S of a
- [(2.1)](#2.1)
[*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]") of a [*lambda-expression*](expr.prim.lambda.general#nt:lambda-expression "7.5.6.1General[expr.prim.lambda.general]"),[*function-body*](dcl.fct.def.general#nt:function-body "9.6.1General[dcl.fct.def.general]"), or [*function-try-block*](except.pre#nt:function-try-block "14.1Preamble[except.pre]"),
- [(2.2)](#2.2)
substatement of a selection or iteration statement
that is not itself a selection or iteration statement, or
- [(2.3)](#2.3)
[*handler*](except.pre#nt:handler "14.1Preamble[except.pre]") of a [*function-try-block*](except.pre#nt:function-try-block "14.1Preamble[except.pre]")
potentially conflicts with a declaration
whose target scope is the parent scope of S,
the program is ill-formed[.](#2.sentence-1)
[*Example [2](#example-2)*: if (int x = f()) {int x; // error: redeclaration of x}else {int x; // error: redeclaration of x} — *end example*]