The problem of template class inheritance

Could you tell me what I am missing?

template <class T> struct Base
{
    T data;
    Base(const T &_data):data(_data) { }
};

template <class T> struct Derived : Base<T>
{
    Derived():Base(T()) {} //error: class 'Derived<T>' does not have any field named 'Base'
};
+3
source share
2 answers
template <class T> struct Derived : Base<T>
{
    Derived():Base<T>(T()) {} 
};
+8
source

The question remains: who is right? GCC is right here. An unqualified name lookup does not consider dependent base classes, so it won’t be Basein Base<T>. You can also change your code to the next standard match option.

Derived():Derived::Base(T()) {}

If I remember correctly, this is only supported by GCC4.5. Earlier versions did not implement a correctly entered class name lookup.

+1
source

All Articles