Edw*_*uez 4 c++ templates c++11
我正在尝试做一个调用另一个类的模板静态方法的模板方法,但是我得到了一些编译错误.最小的情况如下.
如果我编译下面的代码
template<class E, class D>
int foo() {
return D::bar<E>() + 1;
}
Run Code Online (Sandbox Code Playgroud)
它抛出以下输出
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;
Run Code Online (Sandbox Code Playgroud)
当我替换D::bar<E>为D::bar,编译传递所以它似乎与函数的模板参数有一些解析问题.像其他情况一样,我认为它需要一些using或typename黑客来使其工作.
您需要指定依赖名称bar是模板:
return D::template bar<E>() + 1;
// ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
有关和关键字的更多信息,请参阅此问题.typenametemplate