Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When overloading the equality operator==, should one check for reference equality first?

When overloading operator==(), I currently do

bool operator==(const X& lhs, const X& rhs)
{
    return &lhs == &rhs || /* member comparisons ... */;
}

meaning I check first for reference equality of the objects and then for equality for all the members. Is this the "proper" way to do it? Are there any gotchas or things to remember? Is this implementation the most efficient way to do it?

like image 695
Timo Türschmann Avatar asked Sep 29 '22 07:09

Timo Türschmann


1 Answers

Self comparison is usually rare, unless you are an immutable internal pImpl that is usually only compared via == in an unordered_ container after a std::hash collision of your regular wrapper, or something similar.

Optimizing for rare cases is often a bad idea. It adds complexity to the code, and increases testing overhead.

An exception can exist if the comparison is expensive or boundlessly expensive, so that the cost of a failed pointer comparison at the start is trivial. Another exception is where self comparison is unusually expensive, and almost-self (which would be almost as expensive) is exceedingly rare, while self comparison sometimes happens.

The short answer is, keep your code simple, worry about large-order blowups, don't needlessly pessimize, and optimize after actually noticing a performance problem (even if it is just "wow, that was slow", or Monte Carlo profiling).

like image 154
Yakk - Adam Nevraumont Avatar answered Oct 04 '22 04:10

Yakk - Adam Nevraumont