C ++ using-declaration for non-piggy templates

After reading several answers to SO (for example, here and here ), I found out two common alternatives for calling a function template in the template database:

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 an equivalent declaration using Base<T>::foofor non-template functions? Maybe something like

template<int N>
using Base<T>::template get<N>;  //does not compile in gcc
+4
source share
2 answers

Alternatively, usingyou can update 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>(); }

TC VS2015, , .


+2

using. , , . , .

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; 
} 
+1

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


All Articles