template<typename T>
struct self
{
typedef T type;
};
template<class T>
class A
{
struct Type { int x; };
};
template<class T>
class B1 : A<T>
{
Type insert(); // OK
};
template<class T>
class B2 : self< A<T> >::type
{
Type insert(); // syntax error? Why?
};
Asked
Active
Viewed 103 times
0

user541686
- 205,094
- 128
- 528
- 886
-
In conjunction with http://stackoverflow.com/questions/1643035/propagating-typedef-from-based-to-derived-class-for-template. Here's a working sample, the reason explained in the question link: http://ideone.com/Zk5vt – chris Jul 24 '12 at 04:05
-
possible duplicate of [Inheritance and templates in C++ - why are methods invisible?](http://stackoverflow.com/questions/1567730/inheritance-and-templates-in-c-why-are-methods-invisible) – iammilind Jul 24 '12 at 04:14
-
@chris: I made an edit. That doesn't really seem to explain what's happening. – user541686 Jul 24 '12 at 04:18
-
@Mehrdad, [ideone](http://ideone.com/4zH9W) gives an error for your OK line, and so does GCC 4.7.1. – chris Jul 24 '12 at 04:22
-
@chris: So it's a bug in VC++? – user541686 Jul 24 '12 at 04:22
-
@Mehrdad, I honestly wouldn't be surprised. It's clearly stated in the standard why it shouldn't work. – chris Jul 24 '12 at 04:23
-
@chris: Ugh, it seems like something so basic that I *am* surprised a compiler would mess it up... although in their defense, I guess compilers don't have an obligation to reject invalid code... :\ – user541686 Jul 24 '12 at 04:24
1 Answers
1
Visual C++ has non-standard name lookup rules for templates; it's one of the three non-compliances they document here (third paragraph).
I believe all lookups are deferred until the template is instantiated. At that time, all dependent names are available, and so the usual template
and typename
keywords and explicit member access are often not necessary; and this compiler doesn't bother to enforce them.
In both of your derived classes, Type
is a dependent name and so must (when using a compiler that uses standard two-stage lookup) be qualified with typename
:
typename Type insert();

Community
- 1
- 1

Mike Seymour
- 249,747
- 28
- 448
- 644