-1

I'm porting some code from MSVC to GCC and stuck on this problem.

I have a derived class template (Derived) that calls base class template's (Base) methods. If this is non-template method, I can simply say using Base::regular_function, but for template methods this trick does not work anymore and I have to explicitly write this->template template_function() at every call site. Is there a way to avoid this?

Base class template:

template<class T>
class Base
{
public:
    template<class U>
    U * template_function() const { return nullptr; }
    int * regular_function() const { return nullptr; }
};

Derived class template:

template<class T>
class Derived : Base<T>
{
    using base_t = Base<T>;

public:
    using base_t::regular_function;
    using base_t::template_function;

    void foo()
    {
        // OK, works because of "using" above
        auto * regular_ptr = regular_function();

        // Does not work :(
        auto * ptr1 = template_function<int>();

        // Want to avoid
        auto * ptr2 = this->template template_function<int>();
    }
};

It's possible to avoid this by introducing helper function, but I want to avoid writing it in every derived class, too.

template<class U>
U * template_function() const
{
    return base_t::template template_function<U>();
}

https://godbolt.org/z/Mxc14WE8b

UPD: the question is not about why and when should I use template and typename keywords, but about practical advices to reduce the amount of code I need to rewrite. None of the other questions consider this usage.

Genjutsu
  • 384
  • 7
  • 13

0 Answers0