1

Suppose we have

template<typename T>
class Base
{
    ...

    protected:

        template<typename U>
        void compute(U x);

    ...
};

Now I want to call this method from a derived class. With non-template member functions I would normally use using ... declaration or access members with this->.... However, it's not clear how to access template members:

template<typename T>
class Derived : public Base<T>
{
    // what's the correct syntax for
    // using ... template ... Base<T>::compute<U> ... ?

    ...

    void computeFloat()
    {
        float x = ...;
        compute<float>(x);
    }

    void computeDouble()
    {
        double x = ...;
        compute<double>(x);
    }

    ...
};
kfx
  • 8,136
  • 3
  • 28
  • 52
neek
  • 1,210
  • 1
  • 9
  • 10

1 Answers1

1

Even easier. You can write:

void computeFloat() {
    float x = .1;
    this->compute(x);
}

Type is auto-deduced.

EDIT

For the general case, when the type cannot be deduced, you can use either:

Base<T>::template compute<float>();

Or:

this->template compute<float>();

For the examples I've used a compute function with no parameter.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • Thanks, but what about a general case when it cannot be auto-deduced? I gave a toy example and, unfortunately, in my case it doesn't deduce the template parameter. – neek Jan 01 '16 at 15:12