4

Having read several answers on SO (e.g. here and here), I figured out the two usual alternatives for calling a function template in a template base:

template<typename T>
struct Base
{
    template<int N>
    auto get() const
    {
        return N;   
    }
};

template<typename T>
struct Derived : public Base<T>
{
    //first alternative
    auto f0() const { return this-> template get<0>(); }   

    //second alternative
    auto f1() const { return Base<T>::template get<1>(); }    
};

DEMO

But is there also an equivalent to the using Base<T>::foo declaration for non-template functions? Maybe something like

template<int N>
using Base<T>::template get<N>;  //does not compile in gcc
Community
  • 1
  • 1
davidhigh
  • 12,239
  • 1
  • 34
  • 64

2 Answers2

2

As an alternative to using you might redeclare the function with something like:

template<int N> auto get() const{ return Base<T>::template get<N>(); }

This code works with VS2015, but not with coliru:

using Base<T>::template get;
template<int N>
auto f3() { return get<N>(); }

From my understanding after reading the commenty by T.C. this is a custom extension of VS2015 and the behaviour is not part of the standard and might even be considered as ill-formed.


Community
  • 1
  • 1
Simon Kraemer
  • 5,013
  • 1
  • 16
  • 43
1

I wasn't able to get it to work with your using either. However, if the intent is to simplify the cumbersome invocation syntax, then you might find the following alternative useful. I think it gives a similar effect.

template<typename T> 
struct Base 
{ 
    template<int N> 
    auto get() const 
    { 
        return N;    
    } 
}; 

template<typename T> 
struct Derived : public Base<T> 
{ 
    auto f0() const  
    {  
        auto get_0 = Base<T>::template get<0>; 

        get_0(); 
    }    

    //second alternative 
    auto f1() const  
    {  
        auto get_1 = Base<T>::template get<1>; 

        get_1(); 
    }     
}; 

int main() 
{ 
    return 0; 
} 
Jarod42
  • 173,454
  • 13
  • 146
  • 250
Ami Tavory
  • 66,807
  • 9
  • 114
  • 153
  • Thanks for your effort, this is clear. One can also define `template auto get() const {return this-> template get(); }` at class scope (and possibly the equivalent for a non-const version of get). The question was explicitly about the templated using declaration. – davidhigh Feb 04 '16 at 10:35