C++ 从模板函数调用静态模板方法

C++ 从模板函数调用静态模板方法,c++,templates,c++11,C++,Templates,C++11,我正在尝试使用一个模板方法调用另一个类的模板静态方法,但是我遇到了一些编译错误。最基本的情况如下 如果我编译下面的代码 template<class E, class D> int foo() { return D::bar<E>() + 1; } 模板 int foo(){ 返回D::bar()+1; } 它抛出以下输出 g++ -std=c++11 test.cpp -c test.cpp: In function ‘int foo()’: test.cpp:

我正在尝试使用一个模板方法调用另一个类的模板静态方法,但是我遇到了一些编译错误。最基本的情况如下

如果我编译下面的代码

template<class E, class D>
int foo() {
  return D::bar<E>() + 1;
}
模板
int foo(){
返回D::bar()+1;
}
它抛出以下输出

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;
g++-std=c++11 test.cpp-c
test.cpp:在函数“int foo()”中:
test.cpp:4:18:错误:在“>”标记之前应该有主表达式
返回D::bar()+1;
^
test.cpp:4:20:错误:在“')标记之前应该有主表达式
返回D::bar()+1;

当我将
D::bar
替换为
D::bar
时,编译将通过,因此函数的模板参数似乎存在一些解析问题。与其他情况一样,我认为它需要一些
使用
typename
hack才能正常工作。

您需要指定从属名称
是一个模板:

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

有关
类型名
模板
关键字的更多信息,请参阅