Question

There is a quote from the standard, sec 3.3.1/1:

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.

And there is example from the standard, sec 3.3.1/2

int j = 24;
int main() 
{
    int i = j, j;
    j = 42;
}

the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }.

But this two paragraphs are contradictive in the declarative region definition, because j may not be used in the entire example as unqualified name ro refer to the same entity.

Was it helpful?

Solution

While the definition is a bit fuzzy, the usage of the term indicates that it means "the largest part of the program in which that name would be valid, if not hidden by another entity with the same name in a nested region". Of course, the name's still valid if the outer entity is hidden; the only problem with the standard definition is the bit about referring to the same entity.

In practice, that means the whole of a namespace, block or class definition (including its member definitions), and probably a couple of other things that I can't think of right now.

OTHER TIPS

The C++ Committee tries to do all that other programmers could not understand the Standard. Moreover the Committee openly ignores proposals that were submitted by non-members of the Committee

If do not take into account these confusing statements and to apply the common sense then the declarative region either a namespace, function scope or code block scope and so on (for example class scope).

The declarative region of the second j is the outermost code block of the function. In fact in the Standard there is written

The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i.

"all the text between { and }" corresponds to the outermost code block of the function.

So the declarative region is the region (namespace, function scope, code block scope or even function parameter scope) where the variable is declared and could be used as unqualified name. However its potential scope begins from the point of the declaration. Thus the declarative region is always greater than or at least equal to the potential scope of a variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top