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

Loï*_*ier 31 c++ templates g++ c++11

我正在构建一些输入检查器,需要具有整数和/或双精度的特定函数(例如'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 = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >
   inline static U readVal()
   {
      return BuffCheck.getDouble();
   }
};
Run Code Online (Sandbox Code Playgroud)

然后我有以下错误:

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()’
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚第二个版本有什么问题.

Joh*_*itb 34

默认模板参数不是模板签名的一部分(因此两个定义都尝试两次定义相同的模板).但是,它们的参数类型是签名的一部分.所以你可以做到

template <class T>
class check
{
public:
   template< class U = T, 
             typename std::enable_if<std::is_same<U, int>::value, int>::type = 0>
   inline static U readVal()
   {
      return BuffCheck.getInt();
   }

   template< class U = T, 
             typename std::enable_if<std::is_same<U, double>::value, int>::type = 0>
   inline static U readVal()
   {
      return BuffCheck.getDouble();
   }
};
Run Code Online (Sandbox Code Playgroud)

  • @Alan这是不正确的.两者都不是签名的一部分.我写的`= 0`只是为了让用户不自己传递`0`(或者,你也可以写'...`而不是`= 0`,这也不需要参数,但是默认为空包.我认为你和我更喜欢int更好).SFINAE的工作本质上不需要.在这两种情况下它也是"= 0",所以如果它是签名的一部分,那么无论如何都无法区分我的例子=) (3认同)

mfo*_*ini 9

问题是编译器看到同一方法的2个重载,两个都包含相同的参数(在本例中为none)和相同的返回值.你不能提供这样的定义.最简单的方法是在函数的返回值上使用SFINAE:

template <class T>
class check
{
public:
   template< class U = T>
   static typename std::enable_if<std::is_same<U, int>::value, U>::type readVal()
   {
      return BuffCheck.getInt();
   }

   template< class U = T>
   static typename std::enable_if<std::is_same<U, double>::value, U>::type readVal()
   {
      return BuffCheck.getDouble();
   }
};
Run Code Online (Sandbox Code Playgroud)

这样,您提供了2种不同的重载.一个返回一个int,另一个返回一个double,只有一个可以使用某个T进行实例化.


JVA*_*pen 7

我知道这个问题是关于std::enable_if,但是,我想提供一种替代解决方案来解决相同的问题,而无需启用enable_if。它确实需要 C++17

template <class T>
class check
{
public:
   inline static T readVal()
   {
        if constexpr (std::is_same_v<T, int>)
             return BuffCheck.getInt();
        else if constexpr (std::is_same_v<T, double>)
             return BuffCheck.getDouble();
   }   
};
Run Code Online (Sandbox Code Playgroud)

这段代码看起来更像是在运行时编写的。所有分支都必须语法正确,但语义不必如此。在这种情况下,如果 T 是 int,则 getDouble 不会导致编译错误(或警告),因为编译器不会检查/使用它。

如果函数的返回类型太复杂,您可以随时使用它auto作为返回类型。