When are stateless class functors useful in place of a c style function?

前端 未结 5 464
猫巷女王i
猫巷女王i 2021-01-05 08:38

I\'ve found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator().

相关标签:
5条回答
  • 2021-01-05 08:59

    In template libraries where the state of functor argument is not known at definition time, argument of class type provides a more generic interface than none-instance function pointers. STL is a great example where the statefull or stateless perdicates and functors can be used as parameters to classes and functions. If template programming is not part of the plan, function pointer is superior to stateless functor class; a single instance of function can accept all function pointers of a specific signature and code size is minimized. But in case there is a minimal probability of future library extension, functor makes it more generic.

    0 讨论(0)
  • 2021-01-05 09:02

    One reason is run-time efficiency. If you pass a pointer to a function, the compiler has to be unusually clever to produce code for that function inline. Passing an object that defines operator() makes it much easier for the compiler to produce the code inline. Especially for something like sorting, this can increase speed quite substantially.

    In C++11, another reason to use a class is for convenience -- you can use a lambda expression to define the class.

    0 讨论(0)
  • 2021-01-05 09:03

    The typical reason is that when you do this:

    bool less_than(const Point&, const Point&);
    // ...
    std::sort(..., &less_than);
    

    The template argument for the predicate is the following:

    bool(const Point&,const Point&)
    

    Since the sort function receives a function pointer, it is more difficult for the compiler to inline the predicate use inside std::sort(). This happens because you could have another function

    bool greater_than(const Point&, const Point&);
    

    which has the exact same type, meaning the std::sort() instatiation would be shared between the two predicates. (remember that I said that it makes inlining more difficult, not impossible).

    In contrast, when you do this:

    struct less_than {
        bool operator()(const Point&, const Point&) const;
    };
    // ...
    std::sort(..., less_than());
    
    
    struct greater_than {
        bool operator()(const Point&, const Point&) const;
    };
    // ...
    std::sort(..., greater_than());
    

    The compiler generates a unique template instantiation for std::sort() for each predicate, making it easier to inline the predicate's definition.

    0 讨论(0)
  • 2021-01-05 09:23

    Others have made good points about the ability for the compiler to inline the functor. One other possibile advantage of functor objects vs. function pointers is flexibility. The functor might be a template, maybe a derived class, perhaps it has run time configuration (even if stateless at the time operator() is called etc.

    0 讨论(0)
  • 2021-01-05 09:26

    Another reason is that sometimes one comparison function is not enough. Let's say we have a vector of pointers:

    struct X { string name; };
    vector<shared_ptr<X>> v;
    

    Now if we want to sort the vector by name, we have to define our own predicate for the sort function:

    struct Cmp1
    {
        bool operator()(const shared_ptr<X>& left, const shared_ptr<X>& right) const
        { return left->name < right->name; }
    };
    

    That's cool, but what do we do when we need to find the objects with specific name? To work with equal_range, the predicate needs to have two different comparison functions:

    struct Cmp2
    {
        bool operator()(const shared_ptr<X>& left, const string& right) const
        { return left->name < right; }
    
        bool operator()(const string& left, const shared_ptr<X>& right) const
        { return left < right->name; }
    };
    

    This allows us to call equal_range with a string name object:

    equal_range(v.begin(), v.end(), name, Cmp2())
    
    0 讨论(0)
提交回复
热议问题