I have the following code and it fails to compile
template < typename T >
class Base
{
public:
typedef T * TPtr;
void func()
{
}
};
template < typename T >
class Derived : public Base< T >
{
public:
using Base< T >::TPtr;
using Base< T >::func;
TPtr ptr;
};
int main( int c, char *v[] )
{
Derived< int > d;
d.func();
}
The compiler issues the following.
t.cpp:16: error: 'TPtr' does not name a type
t.cpp:16: note: (perhaps 'typename Base<T>::TPtr' was intended)
Now I know I could simply do as the compiler is suggesting but I can’t understand why the
using Base< T >::TPtr;
doesn’t work.
If I comment out the “TPtr ptr” line then it compiles, proving that the “using Base< T >::func;” statement works.
Any ideas?
Base< T >::TPtris a so-called dependent name so you need to prefix it withtypenameto make the declaration work.Additionally,
usingdoesn’t work withtypenameso you need to use atypedefinstead:The issue is that the compiler can’t decide – without knowing what
Tis! – whetherTPtrin this context names a type or a variable/function. To avoid ambiguities, it always assumes the latter, unless explicitly told otherwise (hence the need fortypename).