简体   繁体   中英

Name lookup rules for nested-name-specifier

I've read the following (3.4.3/1):

If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types.

What is the lookup rule for nested-name-specifier?

For example:

#include <iostrem>

namespace A
{
    int j=5;
}

int main()
{
    std::cout << A::j //I assume that A will be searched as just *unqualified-name* appeared inside the function which is a member of namespace
}

The second example:

namespace A
{
    namespace B
    {
        int j=5;
    }
}

int main()
{
    std::cout << A::B::j
}

Is it true that in the second example A::B will be looking as qualified name inside the namespace? Ie we can define rules for nested-name-specifier lookup inductively. But I cant find anything like that in the standard. Is it true at all?

Yes, it's inductive, and I'd say it simply follows from the wording. First, let's add full parenthesizing based on the associativity of :: :

(std::cout) << ((A::B)::j)

(The above is just to demonstrate how the parser understands precedence, it's not valid code).

So j is qualified by the name A::B . It's a qualified name, so it's looked up according to 3.4.3.

A::B is itself a qualified name (it conforms to the syntactic form outlined by 5.1.1/8), so it is looked up according to rules for a qualified name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM