C++ 用类型检查实现std::vector的加法运算符

C++ 用类型检查实现std::vector的加法运算符,c++,overloading,operator-keyword,stdvector,C++,Overloading,Operator Keyword,Stdvector,这是我对std::vector的+运算符的实现 有办法吗 谢谢。你说: 但是我想在实现中添加一种类型检查,这样这个+操作将只针对数字类型调用(例如int,double…) 您可以添加一个静态断言 template <typename T> std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2) { static_assert(std::is_arit

这是我对
std::vector
+运算符的实现

有办法吗

谢谢。

你说:

但是我想在实现中添加一种类型检查,这样这个
+
操作将只针对数字类型调用(例如
int
double
…)

您可以添加一个
静态断言

template <typename T> 
std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2)
{
   static_assert(std::is_arithmetic<T>::value, "Need arithmetic type");
模板
标准::向量运算符+(标准::向量v1,常量标准::向量和v2)
{
静态_断言(std::is_算术::value,“需要算术类型”);

看起来像是直接应用类型特征的例子:

#include <type_traits>

template <typename T> 
std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2)
{
    if(v1.size() != v2.size())
    {
        throw std::exception("Two vector size must be same");
    }

    if (!std::is_arithmetic<T>::value)
    {
        throw std::exception("Only arithmetic vectors supported");
    }

    for(unsigned int i = 0; i<v1.size(); i++)
    {
        v1[i] += v2[i];
    }
    return v1;
}
#包括
样板
标准::向量运算符+(标准::向量v1,常量标准::向量和v2)
{
如果(v1.size()!=v2.size())
{
抛出std::exception(“两个向量大小必须相同”);
}
如果(!std::is_算术::值)
{
抛出std::exception(“仅支持算术向量”);
}

对于(unsigned int i=0;我想你需要一个
std::valarray
来了解操作的实际操作。无论如何,请参阅。为什么要在函数中施加人为限制?你的代码当前可以与定义了
运算符+=
的任何代码一起工作。如果有任何问题,我认为你应该让代码更通用,允许它工作使用不同类型的向量。只有数字类型的限制可能会出现在不同的代码部分…@chris.enable_if对我来说是新的。谢谢。我会读到它。为什么你希望这个o出现运行时异常。对于在编译时很容易捕获的情况,它在运行时会失败。而且,每个人都讨厌
valarray
,因为这是垃圾。@Puppy编译时案例已经添加。如果
valarray
如此糟糕,为什么它没有被弃用呢?公平地说,我没有注意到你额外的静态断言案例。而且,委员会也没有弃用很多可怕的事情,比如
new
@Puppy有一个链接解释它是垃圾吗?它分配的资源太多了吗?@Sahu,谢谢。但是我的代码无法编译它,因为这个错误是
错误C2338:需要算术类型
。我使用的是VS 2010。@JaeJunLee:这就是静态断言失败,因为你传入了一个非算术类型。我想VS2010中的特性可能很容易出错,所以你可能需要替换它。@JaeJunLee,如果我们要通过,
std::is\u arithmetic
应该可以与VS 2010一起使用。@Sahu,很抱歉我不知道
static\u assertive
。这是编译时解决方案!谢谢。所以答案没有问题。
//main function 2
std::vector<std::string> a,b,c;

a.assign(4,"this");
b.assign(4,"is awesome!");

try{
    c = a+b;
}
catch(std::exception& e)
{
    std::cout<<e.what();
    return -1;
}
template <typename T> 
std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2)
{
   static_assert(std::is_arithmetic<T>::value, "Need arithmetic type");
#include <type_traits>

template <typename T> 
std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2)
{
    if(v1.size() != v2.size())
    {
        throw std::exception("Two vector size must be same");
    }

    if (!std::is_arithmetic<T>::value)
    {
        throw std::exception("Only arithmetic vectors supported");
    }

    for(unsigned int i = 0; i<v1.size(); i++)
    {
        v1[i] += v2[i];
    }
    return v1;
}
#include <type_traits>

template <typename T> 
std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2)
{
    static_assert(std::is_arithmetic<T>::value, "Our vector operator + is intended for arithmetic types only");

    if(v1.size() != v2.size())
    {
        throw std::exception("Two vector size must be same");
    }

    for(unsigned int i = 0; i<v1.size(); i++)
    {
        v1[i] += v2[i];
    }
    return v1;
}