I decided to test one of the examples in "Effective C++" and I'm not getting the result I expected. So, apparently this (simplified) code shouldn't compile:
template <class T>
struct A {
void f(){}
};
template <class T>
struct B : public A <T> {
void f2() { f(); } // calling base function - will not compile
};
Here's the explanation (class names changed for simplicity) :
The code above won't compile, at least not with conformant compilers. Such compilers will complain that
f
doesn't exist. We can see thatf
is in the base class, but compilers won't look for it there.We need to understand why. The problem is that when compilers encounter the definition for the class template
B
, they don't know what class it inherits from. Sure, it'sA<T>
, butT
is a template parameter, one that won't be known until later (whenB
is instantiated). Without knowing whatT
is, there's no way to know what the classA<T>
looks like. In particular, there's no way to know if it has af
function.
My compiler (Visual Studio) doesn't mind... It doesn't even show any warnings.
Is the above code correct or not?