Edwin Rodríguez
Edwin Rodríguez

Reputation: 1257

Calling a static template method from a template function

I'm trying to do a template method that calls a template static method of another class, but I get some compiling errors. The minimal case is the following.

If I compile the code below

template<class E, class D>
int foo() {
  return D::bar<E>() + 1;
}

it throws the following output

g++ -std=c++11 test.cpp -c
test.cpp: In function ‘int foo()’:
test.cpp:4:18: error: expected primary-expression before ‘>’ token
   return D::bar<E>() + 1;
                  ^
test.cpp:4:20: error: expected primary-expression before ‘)’ token
   return D::bar<E>() + 1;

When I replace D::bar<E> with D::bar, the compilation pass so It seems there is some parsing problem with the template argument of the function. Like other cases I think it needs some using or typename hack to make it work.

Upvotes: 5

Views: 1482

Answers (1)

TartanLlama
TartanLlama

Reputation: 65770

You need to specify that the dependent name bar is a template:

return D::template bar<E>() + 1;
//        ^^^^^^^^

Live Demo

See this question for more information about the typename and template keywords.

Upvotes: 7

Related Questions