should“operator!=”总是通过“operator ==”来实现。在C ++? [英] Should "operator !=" always be implemented via "operator ==" in C++?

查看:155
本文介绍了should“operator!=”总是通过“operator ==”来实现。在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前审查一个旧的C ++代码库,看到很多代码如下:

  bool SomeClass :: operator = =(const SomeClass& other)const 
{
return member1 == other.member1&& member2 == other.member2;
}

bool SomeClass :: operator!=(const SomeClass& other)const
{
return member1!= other.member1 || member2!= other.member2;
}

清楚地比较逻辑是重复的,上面的代码可能必须更改。



AFAIK,实现 operator!= 的典型方法是这样:

  bool SomeClass :: operator!=(const SomeClass& other)const 
{
return! == other);
}

在后一种情况下,因为它只是调用 operator == ,它将自动反映在 operator!=



是否有任何共鸣的情况, operator!= 应该以任何其他方式实现,除了在C ++代码中重用运算符==

解决方案

的语义 c
$ b

对于所有其他运算符也是如此: a 应等于! > b)!(a == b || a> b) a <= b &&& !(a == b)等等。



为此目的 boost.operators 提供了一些真棒工具来自动生成其他函数的运算符。






但是当你给你的操作符赋予一些特定的语义时(例如:你不使用 == 来检查两个项目是否相同,但是做一些花哨的东西,比如STL,用>> < ;



这种做法一般不建议,虽然甚至STL和许多提升库






EDIT - 稍加添加:



我所说的到目前为止只涉及运算符的语义。如果你决定你的 a!= b 的语义应该是!(a == b)有两种方法来实现它:




  • 通过调用另一个操作符,这是如果你使用boost.operators会发生什么: br>
    bool operator!=(a,b){return!(a == b); }


$

b
$ b

第一种方法通常更容易实现和更安全。最常见的事情可以证明第二个选项是优化,虽然它可能不值得:现代编译器不会增加任何开销在大多数情况下(如果你看到boost.operators源代码,你会看到很多评论,依赖 NRVO 添加开销,或者如果编译器不提供NRVO,它们的代码如何更改) 。



无论你选择什么选项,它都不会对应用程序逻辑产生影响,因为重要的是语义(即:你的操作符的行为,对于任何可能的输入)。


I currently review an old C++ codebase and see a lot of code going like this:

bool SomeClass::operator==( const SomeClass& other ) const
{
   return member1 == other.member1 && member2 == other.member2;
}

bool SomeClass::operator!=( const SomeClass& other ) const
{
   return member1 != other.member1 || member2 != other.member2;
}

clearly comparison logic is duplicated and the code above will likely have to be changed in two places instead of in one.

AFAIK the typical way to implement operator!= is like this:

bool SomeClass::operator!=( const SomeClass& other ) const
{
    return !( *this == other );
}

In the latter case whatever logic change occurs in operator== it will automatically reflected in operator!= since it just calls operator== and performs negation.

Is there any resonable case where operator!= should be implemented in any other way except just reusing operator== in C++ code?

解决方案

In most cases the semantics of a!=b should be equal to !(a==b).

The same is true for about all the other operators: a<b should be equal to !(a=>b) and to !(a==b || a>b) and to a<=b && !(a==b) and so on and so forth.

To this purpose boost.operators offers some awesome tools to automatically generate operators in function of the others.


However when you give some particular semantics to your operators (ie: you don't use == to check whether two items are the same, but to do some fancy stuff like STL does with >> and <<) you could want to give them different implementations.

This practice, in general, is not suggested, although even STL and many boost libraries do that.


EDIT - A little addition:

what I said so far concerns only the semantics of operators. If you decide that the semantics of your a!=b should be !(a==b) you've got two ways to implement it:

  • by calling the other operator, which is what happens if you use boost.operators:
    bool operator!=(a,b) { return !(a==b); }

  • implementing both of them from scratch.

The first method is usually easier to implement and safer. The most common thing which could justify the second option are optimizations, although it's probably not worth it: modern compilers won't add any overhead in most cases (if you look to boost.operators source code you'll see many comments about how they rely on NRVO to add no overhead, or how their code change if compiler doesn't provide NRVO).

Whatever option you choose, anyway, it should make no difference to your application logic, since what matters is the semantics (ie: how your operators behaves, what they return for any possible input).

这篇关于should“operator!=”总是通过“operator ==”来实现。在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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