Wei Li
Wei Li

Reputation: 1897

The scope of a declaration in C++ template specialization

For the following code:

namespace A
{
    struct B
    {
        using type = std::tuple<struct C>;
    };
}

int main()
{
    C* ptr = nullptr;
    B::C* ptr2 = nullptr;
    A::B::C* ptr3 = nullptr;
    A::C* ptr4 = nullptr;
}

I just want to know what is the scope of C. I have tried in gcc 6.5/7.4/8.3/9.1 and clang 6/7/8, they all told me that A::C is correct. But I am not sure whether there are any materials in the C++ standard that describe the scope of C in the above situation.

Could you tell me if you know the materials in the C++ standard that related to this topic? Thanks very much !

Upvotes: 2

Views: 271

Answers (1)

This is detailed in the C++ standard at the following sections:

[basic.lookup.elab]

2 If the elaborated-type-specifier is introduced by the class-key and this lookup does not find a previously declared type-name, or if the elaborated-type-specifier appears in a declaration with the form:

   class-key attribute-specifier-seqopt identifier ;

the elaborated-type-specifier is a declaration that introduces the class-name as described in [basic.scope.pdecl].

[basic.scope.pdecl] (emphasis mine)

7 The point of declaration of a class first declared in an elaborated-type-specifier is as follows:

  • [...]
  • ... if the elaborated-type-specifier is used in the decl-specifier-seq or parameter-declaration-clause of a function defined in namespace scope, the identifier is declared as a class-name in the namespace that contains the declaration; otherwise, except as a friend declaration, the identifier is declared in the smallest namespace or block scope that contains the declaration.

The argument list of a template falls in the "otherwise" clause. A is the smallest namespace that contains the declaration, so the class type C is declared inside of it. A::C is the correct way to refer to it.

Upvotes: 3

Related Questions