As a follow-up to my previous question (Does overriding the operator == for a class automatically override the operator !=),
is there a scenario, where you would override the == and != operators to not be the negation of each other, where the negation would be defined as:
(!(a == b)) == (a != b)
or
(a == b) == (!(a != b))
The logical operator *NOT is used to negate logical variables or constants. *AND and *OR are the reserved values used to specify the relationship between operands in a logical expression.
'Scenario' can suggest a situation already determined or yet to be determined. What's of prime importance is the antecedent— here, the scenario. In a determined situation it is better to use 'when' , otherwise, 'where'.
Strictly speaking, a scenario would be a where, but in informal speech, people frequenty choose when. By the way, I would use especially in place of specially. But note, the sentence doesn't make sense to me, at least without any context. Show activity on this post.
The default operator<=> performs lexicographical comparison by successively comparing the base (left-to-right depth-first) and then non-static member (in declaration order) subobjects of T to compute <=>, recursively expanding array members (in order of increasing subscript), and stopping early when a not-equal result is found
No, in a broad sense. Stepanov would say that if you don’t follow this rule of logic then you get what you deserve. That is why in C++20 they will be by default defined by each other just like he wanted.
In practice, yes, there are at least two scenarios.
As noted in the comment. The logical inconsistency is not exactly between ==
and !=
, but it is related: you can do a = b; assert( a != b);
or have assert( a != a )
.
==
and !=
create two independent expressions. Logically, they should be still be opposites but not in the sense that they are simple Boolean values. Take a look at Boost.Phoenix or Boost.Spirit. These libraries wouldn’t be possible if the language forces to return bools for these operations and one as the negation of the other.In both cases you can reinterpret the situation to "restore" the logical rule. For example 1) you can say that once there is a NaN in your system the program is not in a logical state anymore. 2) you can say that the "expression" a != b
should be also generated by !(a == b)
even if they are not immediately bools.
So, even if the language lets you, you shouldn’t play with the rules of logic.
There is also the myth that supposedly sometimes checking for inequality should be faster than checking for equality or vise versa, as an excuse to implement them separately which can lead to logical inconsistency. This is nonsense given the short circuiting of logical operations that are fundamental to C++ and should be of any system built on top of C++.
This happens all the time when working with databases, where a NULL
value fails every comparison.
So, if you're implementing objects that model data that comes from a database, and you have an object that represents a NULL
value, comparing something for equality with the NULL
value will be false. Comparing it for inequality with a NULL
value will also be false. Every kind of comparison will be false. And, the icing on the cake: comparing a NULL
value for equality with another NULL
value is also false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With