我正在构建一些输入检查器,需要具有整数和/或双精度的特定功能(例如,“isPrime”只能用于整数)。
如果我使用
但是如果我将它用作模板参数(如http://en.cppreference.com/w/cpp/types/enable_if所示)
然后我遇到了以下错误:
我弄不清楚第二个版本有什么问题。
如果我使用
enable_if
作为参数,它完美地工作:template <class T>
class check
{
public:
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0)
{
return BuffCheck.getInt();
}
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, double>::value >::type* = 0)
{
return BuffCheck.getDouble();
}
};
但是如果我将它用作模板参数(如http://en.cppreference.com/w/cpp/types/enable_if所示)
template <class T>
class check
{
public:
template< class U = T, class = typename std::enable_if<std::is_same<U, int>::value>::type >
inline static U readVal()
{
return BuffCheck.getInt();
}
template< class U = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >
inline static U readVal()
{
return BuffCheck.getDouble();
}
};
然后我遇到了以下错误:
error: ‘template<class T> template<class U, class> static U check::readVal()’ cannot be overloaded
error: with ‘template<class T> template<class U, class> static U check::readVal()’
我弄不清楚第二个版本有什么问题。
inline
关键字并不需要,特别是对于同时也是模板的成员更是如此 ;-) - AJG85inline
会影响显式实例化,这可能是好事也可能是坏事。 - Davis Herring