Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sensible scenario, where the == operator is not the negation of the !=

Tags:

c++

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))
like image 433
rabl Avatar asked Apr 05 '21 22:04

rabl


People also ask

Which logical operator is used to negate logical variables or constants?

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.

How do you use scenario in a sentence?

'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'.

What is the difference between a scenario and specially?

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.

What is the default operator for lexicographical comparison?

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


Video Answer


2 Answers

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.

  1. NaN (not-a-number) in IEEE floating point numbers. Two NaN are different and not not equal at the same time.

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 ).


  1. Expression constructs, aka expression templates, aka DSL. == 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++.

like image 173
alfC Avatar answered Oct 30 '22 08:10

alfC


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.

like image 37
Sam Varshavchik Avatar answered Oct 30 '22 09:10

Sam Varshavchik