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

std::enable_if : parameter vs template parameter

本文关键字:参数 if enable std      更新时间:2023-10-16

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

但是如果我将其用作模板参数(如 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()’

我无法弄清楚第二个版本中出了什么问题。

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

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();
   }
};

问题是编译器看到同一方法的 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();
   }
};

这样,您就可以提供 2 种不同的重载。一个返回一个 int,另一个返回一个双精度,并且只有一个可以使用某个 T 实例化。

我知道

这个问题是关于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();
   }   
};

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

如果函数的返回类型很复杂,则始终可以使用auto作为返回类型。