Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

Ugly compiler errors with template

template<typename T>
struct function
{
   typedef T type;
   template<typename U>
   static void f() {}
};

template<typename T>
struct caller
{
        int count;
        caller(): count() {}
        void operator()()
        {
                count++;
                T::f<typename T::type>();
        }
};

int main() {
        caller<function<int> > call;
        call();
        return 0;
}

This seems correct to me, but compiler gives this ugly error which I am unable to understand:

prog.cpp: In member function ‘void caller::operator()()’:
prog.cpp:17: error: expected `(' before ‘>’ token
prog.cpp:17: error: expected primary-expression before ‘)’ token

For your convinence, code is posted here -> http://www.ideone.com/vtP7G

Upvotes: 2

Views: 525

Answers (1)

Thomas Edleson
Thomas Edleson

Reputation: 2196

T::template f<typename T::type>();

Without this "template", the code is parsed as:

T::f [less-than operator] typename T::type [greater-than operator]...

Which is an error.

Upvotes: 3

Related Questions