I'm learning about operator overloading in C++, and I see that == and != are simply some special functions which can be customized for user-defined types. My concern is, though, why are there two separate definitions needed? I thought that if a == b is true, then a != b is automatically false, and vice versa, and there is no other possibility, because, by definition, a != b is !(a == b). And I couldn't imagine any situation in which this wasn't true. But perhaps my imagination is limited or I am ignorant of something?

我知道我可以用一个定义另一个,但这不是我要问的。我也没有问通过值和单位来比较对象之间的区别。或者两个对象是否可以同时相等和不相等(这绝对不是一个选项!这些事情是相互排斥的)。我想问的是:

有没有可能在这种情况下,问两个对象是否相等的问题是有意义的,但问它们不相等的问题是没有意义的?(无论是从用户的角度,还是从实现者的角度)

如果没有这种可能性,那么究竟为什么c++将这两个操作符定义为两个不同的函数呢?


当前回答

通过自定义操作符的行为,您可以使它们按照您的要求进行操作。

你可能希望自定义一些东西。例如,您可能希望自定义一个类。可以通过检查特定属性来比较该类的对象。了解了这种情况后,您可以编写一些只检查最小值的特定代码,而不是检查整个对象中每个属性的每一位。

Imagine a case where you can figure out that something is different just as fast, if not faster, than you can find out something is the same. Granted, once you figure out whether something is the same or different, then you can know the opposite simply by flipping a bit. However, flipping that bit is an extra operation. In some cases, when code gets re-executed a lot, saving one operation (multiplied by many times) can have an overall speed increase. (For instance, if you save one operation per pixel of a megapixel screen, then you've just saved a million operations. Multiplied by 60 screens per second, and you save even more operations.)

Hvd的回答提供了一些额外的例子。

其他回答

通过自定义操作符的行为,您可以使它们按照您的要求进行操作。

你可能希望自定义一些东西。例如,您可能希望自定义一个类。可以通过检查特定属性来比较该类的对象。了解了这种情况后,您可以编写一些只检查最小值的特定代码,而不是检查整个对象中每个属性的每一位。

Imagine a case where you can figure out that something is different just as fast, if not faster, than you can find out something is the same. Granted, once you figure out whether something is the same or different, then you can know the opposite simply by flipping a bit. However, flipping that bit is an extra operation. In some cases, when code gets re-executed a lot, saving one operation (multiplied by many times) can have an overall speed increase. (For instance, if you save one operation per pixel of a megapixel screen, then you've just saved a million operations. Multiplied by 60 screens per second, and you save even more operations.)

Hvd的回答提供了一些额外的例子。

(. .为什么需要两个不同的定义?

需要考虑的一件事是,实现其中一个操作符可能比仅使用另一个操作符的反求值更有效。

(我这里的例子是垃圾,但重点仍然成立,想想bloom过滤器,例如:它们允许快速测试,如果某些东西不在集合中,但测试它是否在集合中可能需要更多时间。)

(. .根据定义,a != b是!(a == b)。

这是你作为程序员的责任。这可能是编写测试的好方法。

有一些非常完善的约定,其中(a == b)和(a != b)都是假的,不一定是相反的。特别地,在SQL中,任何与NULL的比较都会产生NULL,而不是true或false。

如果可能的话,创建这样的新示例可能不是一个好主意,因为这太不直观了,但是如果您试图对现有的约定建模,那么可以选择使您的操作符在该上下文中表现得“正确”。

最后,使用这些操作符检查表达式a == b或a != b是否返回布尔值(true或false)。这些表达式在比较后返回一个布尔值,而不是互斥的。

也许是一个不可比较的规则,其中a != b为假,a == b为假,就像一个无状态位。

if( !(a == b || a != b) ){
    // Stateless
}