相关疑难解决方法(0)

std :: enable_if:参数与模板参数

我正在构建一些输入检查器,需要具有整数和/或双精度的特定函数(例如'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)

c++ templates g++ c++11

31
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

g++ ×1

templates ×1