限制模板功能

Len*_*ers 16 c++ templates

我在http://codepad.org/ko8vVCDF上编写了一个使用模板函数的示例程序.

如何将模板功能限制为仅使用数字?(int,double等)

#include <vector>
#include <iostream>

using namespace std;

    template <typename T>
T sum(vector<T>& a)
{
    T result = 0;
    int size = a.size();
    for(int i = 0; i < size; i++)
    {
        result += a[i];
    }

    return result;
}

int main()
{
    vector<int> int_values;
    int_values.push_back(2);
    int_values.push_back(3);
    cout << "Integer: " << sum(int_values) << endl;

    vector<double> double_values;
    double_values.push_back(1.5);
    double_values.push_back(2.1);
    cout << "Double: " << sum(double_values);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Leo*_*ans 20

这可以通过使用SFINAE来实现,并且通过使用Boost或C++ 11中的帮助程序变得更容易

促进:

#include <vector>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>

template<typename T> 
    typename boost::enable_if<typename boost::is_arithmetic<T>::type, T>::type 
        sum(const std::vector<T>& vec)
{
  typedef typename std::vector<T>::size_type size_type;
  T result;
  size_type size = vec.size();
  for(size_type i = 0; i < size; i++)
  {
    result += vec[i];
  }

  return result;
}
Run Code Online (Sandbox Code Playgroud)

C++ 11:

#include <vector>
#include <type_traits>

template<typename T> 
    typename std::enable_if<std::is_arithmetic<T>::value, T>::type 
        sum(const std::vector<T>& vec)
{
  T result;
  for (auto item : vec)
    result += item;
  return result;
}
Run Code Online (Sandbox Code Playgroud)


Jef*_*man 19

你可以这样做:

template <class T>
class NumbersOnly
{
private:
    void ValidateType( int    &i ) const {}
    void ValidateType( long   &l ) const {}
    void ValidateType( double &d ) const {}
    void ValidateType( float  &f ) const {}

public:
    NumbersOnly()
    {
       T valid;
       ValidateType( valid );
    };
};
Run Code Online (Sandbox Code Playgroud)

如果您尝试创建没有ValidateType重载的NumbersOnly,您将收到错误:

NumbersOnly<int> justFine;
NumbersOnly<SomeClass> noDeal;
Run Code Online (Sandbox Code Playgroud)

  • 我简单地认为这是错误的(抱歉),但方法签名中的&是至关重要的 - 它会阻止实例化的类型只能转换为int.所以char,unsigned int和一个重载operator int()的类都按预期被禁止. (4认同)

Lou*_*nco 18

限制模板的唯一方法是使它使用所需类型的东西,而其他类型则没有.

因此,使用int构造,使用+和+ =,调用复制构造函数等.

任何具有所有这些功能的类型都可以与你的函数一起使用 - 所以,如果我创建一个具有这些功能的新类型,你的函数就可以使用它 - 这很棒,不是吗?

如果要对其进行更多限制,请使用仅为所需类型定义的更多函数.

实现此目的的另一种方法是创建一个特征模板 - 就像这样

template<class T>
SumTraits
{
public:
  const static bool canUseSum = false;
}
Run Code Online (Sandbox Code Playgroud)

然后将它专门用于你想要的类:

template<>
class SumTraits<int>
{
  public:
    const static bool canUseSum = true;
};
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中,你可以写

if (!SumTraits<T>::canUseSum) {
   // throw something here
}
Run Code Online (Sandbox Code Playgroud)

编辑:如评论中所述,您可以使用BOOST_STATIC_ASSERT使其成为编译时检查而不是运行时检查

  • 您还可以使用BOOST_STATIC_ASSERT在编译时强制执行该条件. (3认同)