C++ 覆盖全局运算符逗号给出错误

标签 c++ operators comma-operator

第二个函数给出错误 C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx :“operator”必须至少有一个类类型的形式参数。有什么线索吗?

template<class T,class A = std::allocator<T>> 
class Sequence : public std::vector<T,A> {
 public:
    Sequence<T,A>& operator,(const T& a) {
        this->push_back(a);
        return *this;
    }
    Sequence<T,A>& operator,(const Sequence<T,A>& a) {
        for(Sequence<T,A>::size_type i=0 ; i<a.size() ; i++) {
            this->push_back(a.at(i));
        }
        return *this;
    }
};

//this works!
template<typename T> 
Sequence<T> operator,(const T& a, const T&b) {
    Sequence<T> seq;
    seq.push_back(a);
    seq.push_back(b);
    return seq;
}

//this gives error C2803!
Sequence<double> operator,(const double& a, const double& b) {
    Sequence<double> seq;
    seq.push_back(a);
    seq.push_back(b);
    return seq;
}

最佳答案

在 C++ 中,如果运算符的至少一个参数不是自定义类或非像枚举这样的原始类型,则不能重载运算符。您不能为 int 类型重载 +,同样,您也不能为 double 类型重载 ,

关于C++ 覆盖全局运算符逗号给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406144/

相关文章:

c# - 如果(实例)/自定义类上的隐式 bool 转换

使用 GIN 的 int4range 的 PostgreSQL 索引数组 - 自定义运算符类

c++ - 这个 C++ while 循环是如何工作的?

arrays - C 中 sizeof() 运算符中 "comma"运算符的行为

删除未使用的变量后 C++ hangUp

c++ - 在不使用交换的情况下分配最大缓冲区

c++ - 在 Eclipse C++ 索引中包含所有文件

c++ - 如何让 rdoc 从我的 c 扩展中正确读取方法参数?

javascript - 在 JavaScript 比较中应该使用哪个等号运算符(== vs ===)?

javascript - 逗号运算符什么时候有用?