4

Assume I have a template class TemplateClass with a template function templFcn as follows:

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

Now I would like to write a forward function to emulate this behavior

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
}

Compiler error by 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.

Can someone explain why such template forward failed in this case? And is there any way to circumvent it? Thanks!

Ying Xiong
  • 3,712
  • 5
  • 26
  • 59

1 Answers1

8
v.template templFcn<Bool>(0); // Compiler error, Line 16 (see below)

Dependent clauses need disambiguation, so it knows < is a template clause opener, not less-than.

Yakk - Adam Nevraumont
  • 235,777
  • 25
  • 285
  • 465