Comparison Functor Types vs. operator<

前端 未结 7 2079
庸人自扰
庸人自扰 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条回答
  •  旧时难觅i
    2021-01-02 05:26

    Well, according to the webpage you cite, there's not much advantage for functors ("[operators] can fool our intuition into thinking that expensive operations are cheap, built-in operations.")

    My felling is that one should strive to make you classes first-class objects as much as possible, which, to me, means having them understand as many operators as sensible.

    It's been a while since I written a functor, but it would look something like this:

    class MyClass {....}
    
    class LessThanMyClass : std:binary_function
    {
        public bool operator()(MyClass lhs, MyClass rhs) 
        {   return /* determine if lhs < rhs */ ; }
    }
    
    vector objs;
    std::sort(objs.begin(), objs.end(), LessThanMyClass());
    

    }

提交回复
热议问题