Peter wrote:
> Section 3.3.1 of new C++ standard provides the following definition of
> a declarative region:
>
> "Every name is introduced in some portion of program text called a
> declarative region, which is the largest part
> of the program in which that name is valid, that is, in which that
> name may be used as an unqualified name
> to refer to the same entity.". The definition is followed by this
> example:
>
> int j = 24;
> int main() {
> int i = j, j;
> j = 42;
> }
>
> According to an explanation following the example, the declarative
> region of the first j includes the entire example. How does this not
> contradict the definition of a declarative region? Obviously, the same
> name ("j") can't be used to refer to the same entity in the entire
> example, because first j (global) and second j (defined in main after
> comma) refer to two distinct variables.
The definition of declarative region, as provided in the standard, is «the
largest part of the program (...) in which that name may be used as an
unqualified name to refer to the same entity».
Notice that the definition of «declarative region» is based on the word
"may". In standardeze, there is a difference between "may" and "shall",
which is important in this case. The word "may" represents a possibility
which might not materialise, contrary to the word "shall", which represents
an obligation.
In this case, the standard states that it is possible that, within a
declarative region, a name can be used to refer to the same entity, but it
is also possible that the name won't be used to refer to the same entity.
It's possible, but it doesn't always happen. Hence, the "may".
Then, there is a reason why the «declarative region» concept is complemented
with two additional concepts: potential scope and scope of a declaration.
According to the standard, the «scope» of a declaration is the possibly
discontinuous portion of the program where each particular name is valid,
and the scope differs from the potential scope by accounting for the
possibility that the name may be hidden.
Now, regarding the example, the declarative region of the first j is in fact
the whole program. That's because that, by declaring the first j, the
possibility of accessing that object is extended to the entire example. It
would still be possible to access the first j within the main function if
the second j wasn't declared. Yet, just because the name of the first j was
hidden it doesn't mean that it ceased to be potentially accessible within
that region. The possibility is still there, and if the second j was
renamed then the scope of the first j would again correspond to the
declarative region.
In short, «declarative region» refers to th region where a name is
potentially valid, even when it isn't actually valid. Hence, the example.
Rui Maciel
|