Why explicitly declare the base when calling function when using templates?

3

I have the following code: A derived template calling a function from a base also template.

template <int M>
struct Base { void foo() {} };

template <int M>
struct Derived : public Base<M> {
    void bar() { foo(); }
};

But this causes the following error:

  In% function: In% function main.cpp : void Derived<M>::bar() : error: there are no arguments to main.cpp:7:22 that depends on a template parameter, so a declaration of foo must be available [-fpermissive]

void bar() { foo(); }

The error correction is simple, just use foo instead of the Base<M>::foo(); call.

My question is: Why does this error occur? Why has this restriction been imposed that I must explicitly state the base manually and that the compiler can not deduce this on its own? Why only when using templates? Is my code somewhat ambiguous?

    
asked by anonymous 29.03.2014 / 12:06

1 answer

6

This occurs because the compiler does not look for symbols in base classes dependent on template parameters in name resolution C ++ FAQ .

To do this the compiler would have to wait until the template is instantiated to resolve the names, and once for each instantiation of it, which would make the compilation process (yet) slower.

As specified, the compiler looks for foo while bar is still a template, and as it is not explicitly qualified it does not look in the base class. Even though in the general case it has foo , you could create a specialization of it without this method.

    
29.03.2014 / 13:00