Accessing inherited variable from templated parent class [duplicate]

The later GCC versions correctly implement the standard.

The standard specifies that unqualified names in a template are non-dependent and must be looked up when the template is defined. The definition of a dependent base class is unknown at that time (specializations of the base class template may exist) so unqualified names are unable to be resolved.

This is true for both variable and function names declared in the base class.

As you have observed the solution is to provide the qualified name of the variable or function, or to provide a “using” declaration. E.g.

template<class T> 
int Bar<T>::Perna(int u) 
{ 
  int c = Foo<T>::a * 4; // This works
  c = this->a * 4; // and this

  using Foo<T>::a; 
  c = a * 4; // and with 'using', so should this
}

(I’m actually not 100% sure about the correct syntax for the using version and can’t test from here, but you get the idea).

Leave a Comment