Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does it make sense to have separate implementations for operator== and operator!=?

Tags:

c++

I heard C++ enables overriding both operator== and operator!= because in certain cases a != b can be implemented to be more efficient than !(a == b).

I've thought about this and can't imagine a case where this is true.

What are some examples where it makes sense, performance-wise or other, to have separate implementations for operator== and operator!=?

like image 976
StackedCrooked Avatar asked Jan 25 '26 09:01

StackedCrooked


1 Answers

The first example which comes to mind is implementations analogous to the NULL value for SQL. In that, comparing two objects—either of which are NULL—does not mean they are equal. Only if both are not NULL does it makes sense to return equality.

like image 150
wallyk Avatar answered Jan 26 '26 21:01

wallyk