Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > trouble understanding definition of a declarative region

Reply
Thread Tools

trouble understanding definition of a declarative region

 
 
Peter
Guest
Posts: n/a
 
      12-05-2012
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-05-2012
On 12/5/2012 12:59 PM, 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.
>


What in the definition makes you think that the part of the program that
follows the declaration of the 'j' inside 'main' is included in the
declarative region of the global 'j'?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      12-05-2012
On Dec 5, 6:59*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 12/5/2012 12:59 PM, 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.

>
> What in the definition makes you think that the part of the program that
> follows the declaration of the 'j' inside 'main' is included in the
> declarative region of the global 'j'?


I'm guessing here, partly as I don't know what Peter may be thinking
and partly because I couldn't find N3337 and so am using N3242, but...

Peter seems to think that the part of main following the second j is
*not* part of the declarative region of the global j according to the
definition, because it can't be referred to there and so is on the
face of it contrary to the definition. However, the standard says,
after giving this example, "The declarative region of the first j
includes the entire example."
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-05-2012
On 12/5/2012 3:30 PM, Paul N wrote:
> On Dec 5, 6:59 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> On 12/5/2012 12:59 PM, 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.

>>
>> What in the definition makes you think that the part of the program that
>> follows the declaration of the 'j' inside 'main' is included in the
>> declarative region of the global 'j'?

>
> I'm guessing here, partly as I don't know what Peter may be thinking
> and partly because I couldn't find N3337 and so am using N3242, but...
>
> Peter seems to think that the part of main following the second j is
> *not* part of the declarative region of the global j according to the
> definition, because it can't be referred to there and so is on the
> face of it contrary to the definition. However, the standard says,
> after giving this example, "The declarative region of the first j
> includes the entire example."


Aha... The declarative region is a contiguous area in which the name
*may* be used, and perhaps Peter confuses that with the *scope* that is
not necessarily contiguous. The wording of the Standard is somewhat
convoluted, not easy to comprehend (at least for me). If I were to
explain the difference between the "declarative region" and "scope", I'd
probably be hesitant to find the right words to convey the meaning.
Scopes have everything to do with name resolution, declarative regions
OTOH are larger and can contain code in which the names aren't
necessarily *visible* (because they can be hidden) although they are
necessarily *valid*. The whole point, I believe, is not to confuse the
declarative regions and scopes.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      12-05-2012
On Dec 5, 7:59*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 12/5/2012 12:59 PM, 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.

>
> What in the definition makes you think that the part of the program that
> follows the declaration of the 'j' inside 'main' is included in the
> declarative region of the global 'j'?


According to definition given before the example I'd guess declarative
region
of the global 'j' spans from the start of the example and ends right
before
local 'j' is declared in 'main'. Later the following comment is made
that
contradicts this:

"The declarative region of the first j includes the entire example."




 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      12-05-2012
On Dec 5, 10:02*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 12/5/2012 3:30 PM, Paul N wrote:
>
>
>
>
>
>
>
>
>
> > On Dec 5, 6:59 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >> On 12/5/2012 12:59 PM, 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.

>
> >> What in the definition makes you think that the part of the program that
> >> follows the declaration of the 'j' inside 'main' is included in the
> >> declarative region of the global 'j'?

>
> > I'm guessing here, partly as I don't know what Peter may be thinking
> > and partly because I couldn't find N3337 and so am using N3242, but...

>
> > Peter seems to think that the part of main following the second j is
> > *not* part of the declarative region of the global j according to the
> > definition, because it can't be referred to there and so is on the
> > face of it contrary to the definition. However, the standard says,
> > after giving this example, "The declarative region of the first j
> > includes the entire example."

>
> Aha... *The declarative region is a contiguous area in which the name
> *may* be used, and perhaps Peter confuses that with the *scope* that is
> not necessarily contiguous. *The wording of the Standard is somewhat
> convoluted, not easy to comprehend (at least for me). *If I were to
> explain the difference between the "declarative region" and "scope", I'd
> probably be hesitant to find the right words to convey the meaning.
> Scopes have everything to do with name resolution, declarative regions
> OTOH are larger and can contain code in which the names aren't
> necessarily *visible* (because they can be hidden) although they are
> necessarily *valid*. *The whole point, I believe, is not to confuse the
> declarative regions and scopes.
>

I think the main problem with my reasoning is I though "visible" and
"valid"
were synonymous in this context which is wrong. But then again, what
does
"valid" really mean here? The definition of "valid" ("may be used as
an
unqualified name to refer to the same entity") doesn't enlighten me
enough. Basically, I don't understand how a hidden variable can be
valid.

In example I gave earlier, the global 'j' is obviously hidden from the
point of declaration of local 'j' in 'main' to the end of 'main', so
how is it 'valid' there? How can 'j' be used in that section of code
to refer to global j? I can use "::j", but does it still count as
"unqualified"?





 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      12-09-2012
Any more replies, please? Could you explain to me the concept
of a "valid" name according to the standard? I can't understand
how the global 'j' is "valid" between declaration point of local 'j'
and end of 'main'. In order for global 'j' to be "valid" there,
it should be possible to use the unqualified name 'j' from there
to refer to a global 'j', but that's impossible since global 'j'
is hidden (and therefore inaccessible) there. It can be accessed
by '::j', but that's a qualified name. What am I missing this time?
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      12-09-2012
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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      12-09-2012
Victor Bazarov wrote:

> Aha... The declarative region is a contiguous area in which the name
> *may* be used, and perhaps Peter confuses that with the *scope* that is
> not necessarily contiguous. The wording of the Standard is somewhat
> convoluted, not easy to comprehend (at least for me). If I were to
> explain the difference between the "declarative region" and "scope", I'd
> probably be hesitant to find the right words to convey the meaning.


The differente between declarative region and scope is made quite clear in
the standard. What is left a bit unclear is the difference between
declarative region and potential scope. That bit appears to have been left
out of the standard, and as it stands it appears that they are actually
synonyms.


> Scopes have everything to do with name resolution, declarative regions
> OTOH are larger and can contain code in which the names aren't
> necessarily *visible* (because they can be hidden) although they are
> necessarily *valid*. The whole point, I believe, is not to confuse the
> declarative regions and scopes.


That is true if instead of «declarative region» you refer to «potential
scope». I suspect that the standard defines «potential scope» as a subset
of «declarative region», but it appears the standard isn't clear on what
bits of the «declarative region» are left out of «potential scope».

If «declarative region» and «potential scope» referred exactly to the same
thing then, yes, that assertion would be valid. The potential scope refers
to the portion of the code where a name is potentially valid, and the scope
defines the portion of the code where the name is in fact valid. So,
essentially we get the scope of a name if we take its potential scope and
exclude the potential scope of any other subsequent declaration which might
hide it.


Rui Maciel
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      12-10-2012
Thank you for your detailed answer. Now I know
at least some of the pitfalls that can be
encountered while reading the standard.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
visibility, declarative region, scope, validity, introduction and accessibility Steven T. Hatton C++ 1 12-13-2006 06:29 AM
UK only, Yorkshire region - PC problems, onsite support for Yorkshire region. Need PC upgrading or a new one cheap? Computek Ltd Computer Support 7 02-22-2005 02:38 AM
Convert Region 2 DVD to Region 1? Nsagio Computer Support 4 08-30-2004 02:18 PM
Converting Region 1 to Region 2 Coop DVD Video 0 09-27-2003 10:19 PM
Buffy Region 1 vs. Region 2/4 Anthrax442 DVD Video 1 09-14-2003 04:05 PM



Advertisments