user2953119
user2953119

Reputation:

The name lookup followed after qualified declarator-id

I'm trying to understand what does the following quote mean (3.4.3/3 N3797):

names following the qualified-id are looked up in the scope of the member’s class or namespace.

namespace A
{
    class C
    {
    public:
        static const int a=7;
        static int b;
    };
}

int A::C::b=a; //7

The scope of the static int b; consist only of the declarative region following by the b's point of declaration. Actually:

The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, default arguments, exception-specifications, and brace-or-equal-initializers of non-static data members in that class

This implies that static const int a=7; does not belong to the scope of static int b;. Hence the static const int a=7 cannot be found in the int A::C::b=a;.

It is a typo in the Standard or it is my misunderstanding?

Upvotes: 2

Views: 94

Answers (3)

ecatmur
ecatmur

Reputation: 157354

The rule you quoted (item 1 of 3.3.7p1) is intended for names used within a class:

namespace A
{
    class C
    {
    public:
        int b = a;
        static const int a=7;
    };
}

The rule that permits the code you listed is item 5 of that same paragraph:

5) The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class [...]

There is clearly some overlap between the applicability items 1 and 5 of 3.3.7p1, but that doesn't matter, as they have the same effect where they do overlap.

3.4.3p3 is saying the same as 3.3.7p1 item 5, just with different wording; it clarifies that the type appearing before the qualified-id is not part of the "region defined by [the] member definition".

Upvotes: 0

Columbo
Columbo

Reputation: 60989

This implies that static const int a=7; does not belong to the scope of static int b;. Hence the static const int a=7 cannot be found in the int A::C::b=a;.

No. It implies exactly what you can read there: The potential scope of a name declared in a class also contains function bodies etc. of non-static data members. That does not conflict with the quote above that - the declarative region (and the scope) of a static data member still contains the scope of the class it was declared in itself.

You quoted the relevant part yourself:

names following the qualified-id are looked up in the scope of the member’s class or namespace

Therefore, since in this code-snippet

int A::C::b=a;

a is used after the declarator-id, it is looked up in the class and found.

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254471

The quote says "the scope of the member's class", not "the scope of the member"; so a is looked up in the class scope of C. It can be found there whether or not it's declared after b.

Upvotes: 0

Related Questions