Kyle Kyle - 1 year ago 92
C++ Question

Why does GCC need extra declarations in templates when VS does not?

template<typename T>
class Base
Base() {}
T& get() { return t; }
T t;

template<typename T>
class Derived : public Base<T>
Base<T>::get; // Line A
Base<T>::t; // Line B
void foo() { t = 4; get(); }

int main() { return 0; }

If I comment out lines A and B, this code compiles fine under Visual Studio 2008. Yet when I compile under GCC 4.1 with lines A and B commented, I get these errors:

In member function ‘void Derived::foo()’:

error: ‘t’ was not declared in this scope

error: there are no arguments to ‘get’ that depend on a template parameter, so a declaration of ‘get’ must be available

Why would one compiler require lines A and B while the other doesn't? Is there a way to simplify this? In other words, if derived classes use 20 things from the base class, I have to put 20 lines of declarations for every class deriving from Base! Is there a way around this that doesn't require so many declarations?

Answer Source

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:

  • The declarations you are already using.
  • Base<T>::get()
  • this->get()

(There is also a fourth way, if you want to succumb to the Dark Side:

Using the -fpermissive flag 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 -fpermissive to 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++.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download