How do each of the following behave differently?
I'm unclear what effect const
has on the function parameter, and what the difference in this case would be between passing by value, by reference, or by rvalue reference.
Note that I understand the distinction between pass by value and pass by reference. In the specific case of std::function, or more particularly lambdas, though, I'm not sure what passing the lambda by value vs. passing the lambda by reference does. What would it mean to pass the lambda by value? What is the data that would be copied?
Also is there any practical distinction between const
and not with regard to lambdas?
#include <functional>
void foo_with_func( std::function<void()> f) { ...; f(); ...; }
void foo_with_func( std::function<void()>& f) { ...; f(); ...; }
void foo_with_func( std::function<void()>&& f) { ...; f(); ...; }
void foo_with_func(const std::function<void()> f) { ...; f(); ...; }
void foo_with_func(const std::function<void()>& f) { ...; f(); ...; }
void foo_with_func(const std::function<void()>&& f) { ...; f(); ...; }
All with the same usage:
foo_with_func([&]() { ... });