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

前端 未结 1 793
情歌与酒
情歌与酒 2021-01-18 17:17

When overloading operator==(), I currently do

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


        
1条回答
  •  佛祖请我去吃肉
    2021-01-18 17:56

    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).

    0 讨论(0)
提交回复
热议问题