Finding the name of the first namespace name in inested-name-specifier?

Given the A:: nested name qualifier in a translation block that does not have any classes, templates, enumerations, or typedefs, if correctly formed, the identifier A must refer to the namespace.

For instance:

 namespace X { int i; } int main() { X::i = 42; // Here X:: is a nested-name-specifier // and X is a namespace-name // i is looked up qualified wrt X } 

In the above example, A is X , however my question is about the general case.

What does name lookup look for for namespace name A in A:: ?

The search rules for names are summarized in 3.4, but it is not clear to me how (or which) they are applied in such a situation.

For example, a search for an unqualified name lookup? Does 3.4.1 apply to it?

In other words: what namespaces are looking for a namespace with the name A and in what order? How did you do this with the standard?

+4
source share
2 answers

Yes, A is a regular search for an unqualified name according to 3.4.1, a sequential search of the areas covering a nested qualifier name, except that only classes and namespace names are found. For example, you can get the following:

 int main() { int X; X::i = 42; // OK, int X not found. } 

http://ideone.com/NaEIVd

In fact, the use of this loophole is not recommended; if X were an object of type class, then X::i and Xi could be completely different.


To arrive at this conclusion from the Standard, start with 3.4.3 (a qualified search, which is a high-level construct that we ultimately analyze), which reads in paragraph 1,

The name of a member of a class or namespace or enumerator can be passed after the statement (5.1) resolving the scope applied to the nested qualifier name, which designates its class, namespace or enumeration. If the statement with the resolution in resolution n :: the scope name is not preceded by the decltype specifier, the search for the name preceding this: considers only namespaces, types, and templates whose specialization is types. If the name found does not indicate a namespace or class, enumeration or dependent type, the program is poorly formed.

When reading paragraph 2, this does not apply, since a qualified identifier is not declared. The remaining paragraphs also indicate non-applicable exceptions. So what precedes :: ? Refer to the grammar.

 nested-name-specifier: :: type-name :: namespace-name :: decltype-specifier :: nested-name-specifier identifier :: nested-name-specifier templateopt simple-template-id :: 

Only type-name :: and namespace-name :: match our a priori criteria. They also overlap with what we have found so far. How is type-name or namespace-name usually resolved in a specific context? Unskilled search. Go to 3.4.1.

Firstly, 3.4.1 / 1 is a general rule that should be considered:

In all cases listed in 3.4.1, the search area searches for a declaration in the order specified in each of the relevant categories; the name search ends as soon as an ad is found for the name. If the ad is not found, the program is poorly formed.

The following applicable paragraph is 6:

The name used in the definition of the function following the declarator-id function, which is a member of the namespace N (where, for the purposes of presentation only, N can represent the global scope) must be declared before use in the block in which it is used, or in one of its closing blocks (6.3), or must be declared before it is used in the namespace N or, if N is a nested namespace, must be declared before use in one of the Ns spanning the namespace.

This rule does not tell us about searching the global namespace for a name, but it mentions a function that encompasses the namespace (in this case global) in the list, and paragraph 1 talks about searching the list of namespaces. So, to create a name search.

Not surprisingly, a recursive search prioritizes internal, private namespaces, but you really don't have that kind of thing, so I'll stay here :). It is much faster to leave compilation to the compiler and work manually only in case of a problem.

+4
source

In the example you specified, X will be considered an unqualified name lookup. And as such, he follows the rules in 3.4.1

In this case, the desired namespaces are as follows:

  • The local namespace of the main() function before the definition. (In which the namespace cannot be defined in 7.3.1.4.)
  • Global namespace.

I'm afraid I cannot point to a place in the standard where it specifically says that namespace names are the same as any other name. But they. A name lookup does not know what the namespace name is (it can be a class name or a structure name) until it actually finds a namespace with that name.

+1
source

Source: https://habr.com/ru/post/1489373/


All Articles