template
class Base
{
protected:
Base() {}
T& get() { return t; }
T t;
};
template
class Derived : public Base&l
GCC is right in this case, and Visual Studio mistakenly accepts a malformed program. Have a look at the section on Name lookup in the GCC manual. Paraphrasing:
[T]he call to [
get()] is not dependent on template arguments (there are no arguments that depend on the type T, and it is also not otherwise specified that the call should be in a [template-]dependent context). Thus a global declaration of such a function must be available, since the one in the base class is not visible until instantiation time.
You can get around this in either of three ways:
Base::get() this->get()(There is also a fourth way, if you want to succumb to the Dark Side:
Using the
-fpermissiveflag will also let the compiler accept the code, by marking all function calls for which no declaration is visible at the time of definition of the template for later lookup at instantiation time, as if it were a dependent call. We do not recommend using-fpermissiveto work around invalid code, and it will also only catch cases where functions in base classes are called, not where variables in base classes are used (as in the example above).
But I would recommend against that, both for the reason mentioned in the manual, and for the reason that your code will still be invalid C++.)