阅读了关于 SO 的几个答案(例如 here 和 here ),我想出了在模板库中调用函数模板的两种常用替代方法:
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>(); }
};
但是还有一个等同于using Base<T>::foo
的东西吗?非模板函数的声明?也许像
template<int N>
using Base<T>::template get<N>; //does not compile in gcc
最佳答案
作为 using
的替代方法,您可以使用类似以下内容重新声明该函数:
template<int N> auto get() const{ return Base<T>::template get<N>(); }
此代码适用于 VS2015,但不适用于 coliru:
using Base<T>::template get;
template<int N>
auto f3() { return get<N>(); }
根据我阅读 commenty by T.C. 后的理解这是 VS2015 的自定义扩展,行为不是标准的一部分,甚至可能被视为格式错误。
关于模板基类的非类型函数模板的 C++ using 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197659/