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++将这两个操作符定义为两个不同的函数呢?
当a == b返回bool类型以外的值时,您不会希望语言自动将a != b重写为!(a == b)。有几个原因可以让它这样做。
您可能有表达式构建器对象,其中a == b不会也不打算执行任何比较,而只是构建一些表示a == b的表达式节点。
你可能有惰性求值,其中a == b不会也不打算直接执行任何比较,而是返回某种惰性<bool>,可以在以后的某个时间隐式或显式地转换为bool来实际执行比较。可能与表达式构建器对象结合使用,以便在求值之前完成表达式优化。
你可能有一些自定义的可选<T>模板类,其中给定可选变量T和u,你想允许T == u,但让它返回可选<bool>。
可能还有我没想到的事。即使在这些例子中,操作a == b和a != b都是有意义的,但a != b与!(a == b)不是一回事,因此需要单独的定义。
通过自定义操作符的行为,您可以使它们按照您的要求进行操作。
你可能希望自定义一些东西。例如,您可能希望自定义一个类。可以通过检查特定属性来比较该类的对象。了解了这种情况后,您可以编写一些只检查最小值的特定代码,而不是检查整个对象中每个属性的每一位。
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的回答提供了一些额外的例子。