Reputation: 783
In C++ one can overload the ==
and !=
operators for user types, but the language doesn't care how you do it. You can overload both to return true
no matter what, so !(a==b)
and (a!=b)
don't necessarily have to evaluate to the same thing. How many other languages have a situation where ¬(a = b)
and (a ≠ b)
can be different? Is it a common thing?
It's not just an issue of overloads, but of strange corner cases even for primitive types. NaN in C and C++ doesn't compare equal to anything, including NaN. It is true that NaN != NaN in C, but maybe there are similar cases in other languages that cause ¬(a = b)
and (a ≠ b)
to be different?
Upvotes: 2
Views: 127
Reputation: 9570
Guy L. Steele famously said
...the ability to define your own operator functions means that a simple statement such as x=a+b; in an inner loop might involve the sending of e-mail to Afghanistan.
And as corsiKa says, just because you can do it, doesn't make it a good idea.
Upvotes: 2
Reputation: 3658
I know for a fact that Python and Ruby can and Java and PHP can not. (In Java ==
determines if two objects are the same thing in memory, not just semantically equivalent values. In PHP...never mind.) I'd also imagine that Lisp and JS can and C can not, but that's a bit more speculative.
It's nothing unusual to be able to overload operators. It is very rare for !(a==b)
and (a!=b)
to have different results though.
Upvotes: 1