5

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.

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.