How can I call a member from a templated base class on a templated derived class?

64 views Asked by At

With this setup:

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

class Derived : Base<1> {
    static void bar(Derived *d) {
        //No syntax errors here
        d->Base<1>::foo();
    }
};

Everything works fine. However, with this example:

template<class E>
struct Base {
    void foo();
};

template<class E>
class Derived : Base<E> {
    static void bar(Derived<E> *d) {
        //syntax errors here
        d->Base<E>::foo();
    }
};

I get:

error: expected primary-expression before '>' token
error: '::foo' has not been declared

What's the difference? Why does the second cause a syntax error?

1

There are 1 answers

0
Andy Prowl On

With the premise that your code compiles fine on Clang 3.2 (see here) and GCC 4.7.2 (see here), I do not see a reason for using Base<E>::: just use d->foo():

template<class E>
struct Base {
    void foo() { }
};

template<class E>
struct Derived : Base<E> {
    static void bar(Derived<E> *d) {
        //syntax errors here
        d->foo();
    }
};

int main()
{
    Derived<int> d;
    Derived<int>::bar(&d);
}

Alternatively, you could try using the template disambiguator:

d->template Base<E>::foo();