8

Various containers in the standard library take predicates by value.

For example:

From ยง23.3.5 std::list

template <class Predicate>
void remove_if(Predicate pred);

Is there an advantage over taking predicates as forwarding references?
It might not be all that common, but potentially the functor is heavy to copy.
Is the general reccomendation to take predicates by value, and in that case why?

sp2danny
  • 7,488
  • 3
  • 31
  • 53
  • 2
    Probably the reason is that predicates should be functors that are not heavy to copy (without state, or without "too much state"). Good question! โ€“ vsoftco Mar 10 '17 at 15:17
  • 1
    Related (I'd say almost a dupe): http://stackoverflow.com/q/17234543/3093378 โ€“ vsoftco Mar 10 '17 at 15:33
  • 1
    If it takes it by value as a functor or lambda it can it can inline the call. Taking it by reference is the same as calling through a pointer and therefore the call can not be optimised out. โ€“ Richard Critten Mar 10 '17 at 16:27

0 Answers0