Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of a namespace identifier?

It seems that there is no clear definition over the point of declaration, declarative region, scope of a namespace identifier, except those of an identifier inside a namespace—according to the standard(§3.3.6/1).

The declarative region of a namespace-definition is its namespace-body. The potential scope denoted by an original-namespace-name is the concatenation of the declarative regions established by each of the namespace-definitions in the same...

Although the standard indeed talks about those of a declaration—a namespace-definition is declaration, that is not applicable to the case of namespace-definition because it has no declarator, nor initializer—according to the standard(§3.3.2/1).

The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below...

Then, how can I determine those of a namespace identifier?

like image 681
b1sub Avatar asked Dec 13 '15 04:12

b1sub


1 Answers

From the text you quoted from the standard, my interpretation would be that you have answered your own question.

As you say, a namespace cannot have a complete declarator, since an additional declarative region for it can be created in any compilation unit (i.e. source file or header included by that source file) for any namespace X by namespace X { <new names within this declarative region> }.

Since there can never be a complete declaration of a namespace, there can never be a point of declaration for a namespace. Since there is no point of declaration, there is no such thing as a namespace identifier, and no such thing as a scope of one.

Which means that a namespace is just a label which can be part of an identifier. istream is an identifier within namespace std, the complete name of that identifier (as referenced from code outside a declarative region of namespace std) is std::istream. All the using namespace std; does is, when trying to find a match for a potential identifier foo is tell the compiler to look within namespace std (or the declarative regions it has visibility of) for an identifier named foo which will be a candidate match. [Which is why using namespace for multiple namespaces can result in ambiguity, if more than one namespace contains the same identifier].

like image 64
Peter Avatar answered Sep 18 '22 18:09

Peter