Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ using-declaration for non-type function templates of template base class

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
like image 655
davidhigh Avatar asked Oct 18 '22 16:10

davidhigh


2 Answers

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.


like image 113
Simon Kraemer Avatar answered Oct 21 '22 05:10

Simon Kraemer


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; 
} 
like image 20
Ami Tavory Avatar answered Oct 21 '22 07:10

Ami Tavory