Quick A: They are not, because it is not always true.
Recently on SO:
Are `==` and `!=` mutually dependent?
You would not want the language to automatically rewrite
a != b
as!(a == b)
whena == b
returns something other than abool
. And there are a few reasons why you might make it do that.You may have expression builder objects, where
a == b
doesn't and isn't intended to perform any comparison, but simply builds some expression node representinga == b
.You may have lazy evaluation, where
a == b
doesn't and isn't intended to perform any comparison directly, but instead returns some kind oflazy<bool>
that can be converted to bool implicitly or explicitly at some later time to actually perform the comparison. Possibly combined with the expression builder objects to allow complete expression optimisation before evaluation.You may have some custom
optional<T>
template class, where given optional variablest
andu
, you want to allowt == u
, but make it returnoptional<bool>
.There's probably more that I didn't think of. And even though in these examples the operation
a == b
anda != b
do both make sense, stilla != b
isn't the same thing as!(a == b)
, so separate definitions are needed.
Add a Comment
Comments are closed.