如何最好地重载操作符< > < => =但是只写一个或两个比较函数? [英] How to best overload operator < > <= >= but only write one or two comparison functions?

查看:99
本文介绍了如何最好地重载操作符< > < => =但是只写一个或两个比较函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的属性类型为std :: string的类。我想通过比较属性来为类提供一些比较运算符函数,如<>,==,< =和> =。



我的问题是:任何简单的方法或工具



,例如, (和==),其他可以自动生成。



(2)甚至更简单,因为类比较取决于

解决方案

选项1: std :: rel_ops



需求



较小的比较运算符。其余部分可以使用 std :: rel_ops 自动生成, ,因为以下条件成立:

  a!= b等于!(a == b)
a <= b等于(a a> = b等于!(a< b)
a> b等于!(a <= b)



警告


b $ b

但是,由于很容易自己提供这些运算符,你应该花费额外的精力并编写它们。他们也有一些缺点,如 R。 Martino Fernandes


请注意,[they]无法正常工作,除非:


  1. 在每个使用运算符的上下文中使用命名空间std :: rel_ops 添加

  2. 使用:: operator!=;添加 using :: operator< =; using :: operator> =;在类的命名空间中使用:: operator> ;; (在类的命名空间中使用命名空间std :: rel_ops



选项二:奇怪的重复模板模式



在这种情况下,您提供一个简单的基类,实现所有需要的运算符,并简单地继承它:

  template< class T> 
struct implement_relational_operators {
friend bool operator< =(const T& a,const T& b){return a< b || a == b; }
friend bool operator>(const T& a,const T& b){return!(a <= b); }
friend bool operator!=(const T& a,const T& b){return!(a == b);}
friend bool operator> =(const T& const T& b){return!(a };

template< class T>
struct scalar:public implement_relational_operators< scalar< T> > {
T value;
bool operator<(const scalar& o)const {return value< o.value;}
bool operator ==(const scalar& o)const {return value == o.value;}
};

这不包含 std :: rel_ops 。但是,您仍然需要实现运算符<运算符==


I have a class with an attribute of type std::string. I'd like to provide some comparison operator functions like <, >, ==, <=, and >= for the class by comparing the attribute.

My questions is that: any easy way or tool to

(1) just write one or two functions, such as the one for operator < (and ==), others can be automatically generated.

(2) or even simpler since the class comparison is depending on its attribute of type std::string whose comparison functions are already provided.

解决方案

Option 1: std::rel_ops

Requirements

You just need the equality and the lesser comparison operator. The rest can be generated automatically with std::rel_ops, since the following holds:

a != b equal to !(a == b)
a <= b equal to (a < b) || (a == b)
a >= b equal to !(a < b)
a >  b equal to !(a <= b)

Warning

However, since it's easy to provide those operators yourself you should just take the extra effort and write them. And they also have some drawbacks, as mentioned by R. Martino Fernandes:

Note that [they] won't work properly unless you:

  1. add using namespace std::rel_ops on every context you use the operators; or
  2. add using::operator!=; using::operator<=; using::operator>=; using::operator>; in the namespace of your class (using namespace std::rel_ops in the namespace of your class is not acceptable because it does not get picked up by ADL).

Option 2: Curiously recurring template pattern

In this case you provide a simple base class which implements all needed operators and simply inherit from it:

template <class T>
struct implement_relational_operators{
    friend bool operator<=(const T & a,const T & b){ return a < b || a == b; }
    friend bool operator>(const T &  a, const T & b){ return !(a <= b); }
    friend bool operator!=(const T &  a, const T & b){ return !(a == b);}
    friend bool operator>=(const T &  a, const T & b){ return !(a < b); }
};

template <class T>
struct scalar : public implement_relational_operators<scalar<T> >{
    T value;
    bool operator<(const scalar& o) const { return value < o.value;}
    bool operator==(const scalar& o) const { return value == o.value;}
};

This doesn't share the drawbacks from std::rel_ops. However, you still need to implement operator< and operator==.

这篇关于如何最好地重载操作符&lt; &gt; &lt; =&gt; =但是只写一个或两个比较函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆