Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ calling template functions of Base class

Below are two cases.

Case 1) Base->BaseIndirect->DerivedIndirect

Case 2) Base->Derived

In Case 2), I am able to call a template function of Base class using 3 notations. In Case 1), I am able to call template function of Base class using only 1 of those notations. And, I am NOT able to call template function of BaseIndirect using any notation :(. How do I 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 on 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<'
like image 694
anon Avatar asked Feb 08 '11 05:02

anon


People also ask

What is template class and template function?

Sep 16, 2022. Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.

Can a template base class derived?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

What is the need of template functions in C?

This template allows the code needed to define variables with basic type to be generalized and abstracted. The goal is to make code sharable and similar between types.

What is function template syntax?

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.


1 Answers

The reason that many of these calls are failing is that there's a syntactic ambiguity you need to resolve using the single most obscure 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)

Which is nonsensical. The template keyword explicitly removes this ambiguity. Adding the template keyword into the other cases you mentioned should fix those problems.

I'm actually not sure why this works for direct base classes, so if someone could answer that part of the question I'd love to see what the answer is.

like image 104
templatetypedef Avatar answered Oct 04 '22 21:10

templatetypedef