Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ compiler error with templated base class member

I'm trying to compile some Microsoft Visual C++ code using g++. Now I've come across a compiler error which I really can't understand. The (simplified) code looks like this:

template<int X> struct A {
    template<class Ret> static Ret call() {
        return 0;
    }
};

template<int X> struct B : A<X> {
    int f() {
        return A<X>::call<int>();
    }
};

When I try to compile this with g++ (version 4.4.5), I get the following error:

main.cpp: In member function int B<X>::f(): 
main.cpp:16: error: expected primary-expression before int 
main.cpp:16: error: expected ; before int
main.cpp:16: error: expected unqualified-id before > token

If I remove the template type (Ret) from method A::call, the code compiles just fine. Can anybody see whats wrong here?

Thanks!

like image 256
user1169299 Avatar asked Nov 22 '25 10:11

user1169299


1 Answers

You need the template keyword:

return A<X>::template call<int>();

call is a dependent name, meaning that its signification depends on a template parameter, which is not known when the compiler process f(). You need to indicate that call is a function template by prefixing it with the template keyword.

The same thing happens when you try to access a nested type: you need to add the typename keyword to indicate that the name denotes a type:

template <typename T>
struct A { typedef int type; };

template <typename T>
void f()
{
    typename A<T>::type i = 0; // notice "typename" here
}

And sometimes you even need to mix both:

template <typename T>
struct A
{
    template <typename U>
    struct inner { };
};

template <typename T>
void f()
{
    typename A<T>::template inner<int> var;
}

The use of these two keywords is thoroughly explained in the answers to this question: Where and why do I have to put the “template” and “typename” keywords? (thanks to @Björn Pollex for finding the link).

like image 177
Luc Touraille Avatar answered Nov 23 '25 23:11

Luc Touraille



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!