我正在構建一些輸入檢查器,它需要具有整數和/或雙精度的特定函數(例如'isPrime'應該只能用於整數)。std :: enable_if:參數vs模板參數
如果我使用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();
}
};
,但如果我使用它作爲一個模板paramater(如表現出對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()’
我不能數字o在第二個版本中出了什麼問題。
可能無關緊要,但在VS2010我不能這樣做,因爲默認模板參數只允許類模板 - 我不知道G ++ – David
這是迂腐,但'inline'關鍵字上一個成員方法或模板是不需要的,當然不是一個也是模板的成員;-) – AJG85