dav*_*igh 5 c++ inheritance templates
阅读了有关 SO 的几个答案(例如,此处和此处),我找到了在模板库中调用函数模板的两种常用替代方法:
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>(); }
};
Run Code Online (Sandbox Code Playgroud)
但是是否也有与using Base<T>::foo非模板函数的声明等效的方法呢?也许像
template<int N>
using Base<T>::template get<N>; //does not compile in gcc
Run Code Online (Sandbox Code Playgroud)
我也无法让它与你一起工作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;
}
Run Code Online (Sandbox Code Playgroud)