How to call a member function of a template in the base class of the template?

When calling a non-templated member function in a base class, you can import your name with using into a derived class, and then use it. Is this also possible for template member functions in the base class?

It just doesn't work with using (with g ++ - snapshot-20110219 -std = C ++ 0x):

 template <typename T> struct A { template <typename T2> void f() { } }; template <typename T> struct B : A<T> { using A<T>::f; template <typename T2> void g() { // g++ throws an error for the following line: expected primary expression before `>` f<T2>(); } }; int main() { B<float> b; bg<int>(); } 

I know that the base class prefix is โ€‹โ€‹explicit, as in

  A<T>::template f<T2>(); 

works fine, but the question arises: is this possible without using a simple declaration (as is the case when f not a template function)?

If this is not possible, does anyone know why?

+4
source share
2 answers

it works (pun intended): this->template f<T2>();

So,

 template <typename T> struct B : A<T> { template <typename T2> void f() { return A<T>::template f<T2>(); } template <typename T2> void g() { f<T2>(); } }; 

Why using does not work on template-dependent template functions, itโ€™s pretty simple - the grammar doesnโ€™t allow you to use the required keywords in this context.

+9
source

I believe that you should use:

this->A<T>::template f<T2>();

or

this->B::template f<T2>();

0
source

Source: https://habr.com/ru/post/1342939/


All Articles