C++:转发模板成员函数调用失败

Yin*_*ong 4 c++ templates template-meta-programming c++11

假设我有一个模板类TemplateClass,模板函数templFcn如下:

template <typename T>
struct TemplateClass {
  template <bool Bool> void templFcn(int i) { }
};
void test() {
  TemplateClass<float> v;
  v.templFcn<true>(0);  // Compiles ok.
}
Run Code Online (Sandbox Code Playgroud)

现在我想编写一个forward函数来模拟这种行为

template <typename T, template<typename> class C, bool Bool>
void forward(C<T>& v) {
  v.templFcn<Bool>(0);  // Compiler error, Line 16 (see below)
};

void test2() {
  TemplateClass<float> v;
  forward<float,TemplateClass,true>(v);  // Line 21
}
Run Code Online (Sandbox Code Playgroud)

clang ++的编译器错误:

test.cc:16:5: error: reference to non-static member function must be called
  v.templFcn<Bool>(0);
  ~~^~~~~~~~
test.cc:21:3: note: in instantiation of function template specialization
      'forward<float, TemplateClass, true>' requested here
  forward<float,TemplateClass,true>(v);
  ^
test.cc:3:29: note: possible target for call
  template <bool Bool> void templFcn(int i) { }
                        ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

有人能解释为什么这种模板转发在这种情况下失败了吗 有没有办法绕过它?谢谢!

Yak*_*ont 8

v.template templFcn<Bool>(0); // Compiler error, Line 16 (see below)
Run Code Online (Sandbox Code Playgroud)

依赖条款需要消除歧义,因此它知道<是一个模板子句开启者,而不是小于.