Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: forward a template member function call failed [duplicate]

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!

like image 533
Ying Xiong Avatar asked Feb 11 '23 18:02

Ying Xiong


1 Answers

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.

like image 106
Yakk - Adam Nevraumont Avatar answered Feb 13 '23 08:02

Yakk - Adam Nevraumont