Comparison Functor Types vs. operator<

前端 未结 7 2073
庸人自扰
庸人自扰 2021-01-02 04:54

In the Google C++ Style Guide, the section on Operator Overloading recommends against overloading any operators (\"except in rare, special circumstances\"). Specifi

7条回答
  •  自闭症患者
    2021-01-02 05:33

    Except for the more fundamental types, the less-than operation isn't always trivial, and even equality may vary from situation to situation.

    Imagine the situation of an airline that wants to assign all passengers a boarding number. This number reflects the boarding order (of course). Now, what determines who comes before who? You might just take the order in which the customers registered – in that case, the less-than operation would compare the check-in times. You might also consider the price customers paid for their tickets – less-than would now compare ticket prices.

    … and so on. All in all, it's just not meaningful to define an operator < on the Passenger class although it may be required to have passengers in a sorted container. I think that's what Google warns against.

提交回复
热议问题