Accessing base class template: is the following syntax legal?

36 views
Skip to first unread message

Vincent Reverdy

unread,
Jan 15, 2018, 2:17:39 PM1/15/18
to ISO C++ Standard - Discussion
Hello all,

Consider the following program, which compiles on both g++ and clang:

#include <iostream>
template <class T> struct base {};
struct derived: base<int> {};
int main(int argc, char* argv[]) {
   
typename derived::base<double> x; // <- (1) Is this legal?
   
typename derived::derived::base<double>::base<int>::base<void> y; // <- (2) Is this legal?
   
return 0;
}

Are both marked lines perfectly legal C++?

Thanks,
Vincent

Brian Bi

unread,
Jan 15, 2018, 5:04:57 PM1/15/18
to std-dis...@isocpp.org
I think they are legal. See [temp.local]/1 (emphasis mine)

Like normal (non-template) classes, class templates have an injected-class-name (Clause 12). The injected-
class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as
a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier
of a friend class template declaration, it refers to the class template itself.



--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.



--
Brian Bi

Richard Smith

unread,
Jan 15, 2018, 8:27:18 PM1/15/18
to std-dis...@isocpp.org
The first one is legal. In the second one, I think derived::derived::base<double>::base<int>::base names a constructor, and so is ill-formed; see [class.qual]/2. Lookups of names followed by :: ignore function names, so "derived::derived::" does not name a constructor. But a constructor is a valid lookup result for (2), so that's ill-formed, I think.
Reply all
Reply to author
Forward
0 new messages