C ++ call template functions for base class

The following are two cases.

Case 1) Base-> BaseIndirect-> DerivedIndirect

Case 2) Basic-> Derived

In case 2), I can call the template function of the base class using 3 notation. In case 1), I can call the template function of the base class using only one of these notation. And I can not call the BaseIndirect template function using any notation :( How to fix this? Thanks.

struct Base { template<bool R> inline void fbase(int k) {}; }; template<class ZZ> struct BaseIndirect : Base { template<bool R> inline void fbaseIndirect(int k) {}; }; template<class ZZ> struct DerivedIndirect : BaseIndirect<ZZ> { DerivedIndirect() { this->fbase<true>(5); // gives error, line 13 fbase<true>(5); // gives error, line 14 Base::fbase<true>(5); // WORKS, line 15 this->fbaseIndirect<true>(5); // gives error, line 16 fbaseIndirect<true>(5); // gives error, line 17 BaseIndirect<ZZ>::fbaseIndirect<true>(5); // gives error, line 18 } }; template<class ZZ> struct Derived : Base { Derived() { this->fbase<true>(5); // WORKS fbase<true>(5); // WORKS Base::fbase<true>(5); // WORKS } }; int main() { Derived<int> der; DerivedIndirect<int> derIndirect; }; 

ERRORS at compilation

 test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect()': test.cpp:14: error: 'fbase' was not declared in this scope test.cpp:17: error: 'fbaseIndirect' was not declared in this scope test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect() [with ZZ = int]': test.cpp:34: instantiated from herep test.cpp:13: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<' test.cpp:16: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<' test.cpp:18: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<' 
+7
source share
1 answer

The reason many of these calls fail is because there is syntactic ambiguity that you need to resolve using the single most implicit use of the template keyword. Instead of writing

 this->fbase<true>(5); 

You need to write

 this->template fbase<true>(5); 

The reason is that without the template keyword, the compiler parses this as

 (((this->fbase) < true) > 5) 

It's pointless. The template keyword explicitly removes this ambiguity. Adding the template keyword to the other cases you mentioned should fix these problems.

Actually, I'm not sure why this works for direct base classes, so if someone can answer this part of the question, I would like to see what the answer is.

+14
source

All Articles