我正在构建一些输入检查器,需要具有整数和/或双精度的特定函数(例如'isPrime'应该仅适用于整数).
如果我使用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();
}
};
Run Code Online (Sandbox Code Playgroud)
但如果我将它用作模板参数(如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 …Run Code Online (Sandbox Code Playgroud)