0

I am trying to understand how functions (mainly lambda expressions) as function parameters exactly work. So why are for example comparison functions in the standard library passed by value and not by const&?

4
  • In particular, the accepted answer provides an excellent explanation of a rather obscure reason of why function objects should be passed by value. Jan 28, 2021 at 20:55
  • Does this also apply for functions with acutal arguments? And do I understand correctly that const& should also work, but is less performant?
    – Henk
    Jan 28, 2021 at 21:00
  • Yes and yes: the same reasoning applies regardless of the number of arguments the function object takes, and your understanding about const& is correct. While you are at that page, consider looking at Ben Voigt's concise answer, it's a nice option to keep in mind. Jan 28, 2021 at 21:05
  • The marked answer only talks about std::function. If you pass a function by having a templated function, it fits more to this question: stackoverflow.com/questions/65562986/…
    – n314159
    Jan 29, 2021 at 16:27

1 Answer 1

1

Functors can have state that they modify. If you make the functor const then it can't modify that state.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.