It seems to me that for any composite object, it is inefficient to implement operator!=
in terms of operator==
.
For an object with N
subobjects to compare, operator==
must always perform every one of those comparisons: 2 objects are equal if and only if every sub-object is equal. (Ignoring irrelevant sub-objects for this discussion.)
Compare to operator!=
, in which it is sufficient to check sub-objects only until a mismatch is found. To make operator!=
delegate to operator==
is therefore inefficient in the composite case, as it yields N comparisons in every case, instead of just the worst case.
This is posted as a new question after an overlong comment discussion started by this comment. The other commenter stated:
operator==
does not always require N comparisons, it only requires N comparisons in the worst case, which is when at least all but the last subobjects are equal
I can't see how that can possibly be correct..have I missed something?
This is of course far more general than purely C++, but since the discussion was specific to that I've kept the same tag.