C++ Logo

std-discussion

Advanced search

Re: Template name resolution

From: Andrew Schepler <aschepler_at_[hidden]>
Date: Thu, 14 Mar 2024 08:18:21 -0400
On Wed, Mar 13, 2024 at 11:18 PM Russell Shaw via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> By 'identifier', i assumed the end entity such as 'a' in 'A::B::a'
> The standard could do a bit better to add some more context.
>

Except the 'a' in 'A::B::a' (if used as a name), or in your first example's
'T::U::a', is not part of any nested-name-specifier. See the syntax in
[expr.prim.id.qual]:

*qualified-id*:
    *nested-name-specifier* template<sub>*opt*</sub> *unqualified-id*

*nested-name-specifier*:
    ::
    *type-name* ::
    *namespace-name* ::
    *decltype-specifier* ::
    *nested-name-specifier* :: *identifier*
    *nested-name-specifier* :: template<sub>*opt*</sub> *simple-template-id*
 ::

(Later drafts replace *decltype-specifier* with *computed-type-specifier* to
also deal better with variadic parameter packs.)

The wording about a nested-name-specifier containing a
nested-name-specifier and its identifier or simple-template-id refers to
rules 4 and 5 for the grammar node nested-name-specifier.

In your first example, 'T::U::a' is a qualified-id containing the
nested-name-specifier 'T::U::' and the unqualified-id 'a'. And that
nested-name-specifier 'T::U::' uses the fourth grammar rule, immediately
containing another type-dependent nested-name-specifier 'T::' and the
identifier 'U'. So breaking down the quoted requirement,

"In a *nested-name-specifier*" (T::U::)
"that immediately contains a *nested-name-specifier*" (T::)
"that depends on a template parameter," (the type T in T:: is
type-dependent)
"the *identifier*" (U)
"or *simple-template-id*" (If we had 'T::template X<int>::b' using
grammar rule 5, 'X<int>' would be the simple-template-id.)
"is implicitly assumed to name a type" (T::U:: names a type, to be
determined at template instantiation.)
"without the use of the typename keyword" (which is good, since
'T::typename U::a' is invalid syntax and 'typename T::U::a' makes 'a' a
type, not 'U')

-- Andrew Schepler

Received on 2024-03-14 12:18:33