Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • Does that mean that the third parameter of sort_ints is actually taking an entire object by value? Won't that be bad/inefficient if we're working with large objects?
    – Nav
    Commented Jan 31, 2011 at 5:55
  • 1
    @Nav: yes - a reference would be better. But, a comparison object rarely has much state, and is only passed once regardless of the number of items being sorted, so it's not likely to be a big deal. Commented Jan 31, 2011 at 6:01
  • @Nav- The compiler is usually really good at aggressively optimizing the receiver object out of the picture when invoking operator(). Coupled with the fact that the compiler can resolve which function is being called at compile-time (since it knows the type of the receiver), it's often much faster to use function objects than raw functions! (Though it does make the executable bigger) Commented Jan 31, 2011 at 6:13
  • Thanks. I figured the best way to have the parameters of sort_ints would have been void sort_ints(int* begin_items, int num_items, const ComparisonFunctor& c);
    – Nav
    Commented Jan 31, 2011 at 6:35
  • @Nav: if you use the function object as state machine, your approach won't work.
    – Donotalo
    Commented Jan 31, 2011 at 7:39