Why can I pass an accepting value called by the std :: reference accept function?

When I declare a variable function<void(const Foo&)>, the compiler still allows me to assign a lambda that takes a value:

function<void(const Foo&)> handler;
handler = [](Foo f){};

(cfr. also http://cpp.sh/5dsp )

So, when the handler is called, a copy will be made. Which section of the standard allows this? Is there a way so that I can indicate the client code that this will be a problem (some kind of static_assert?)?

+6
source share
1 answer

Per [ func.wrap.func.con ]

std::function<R(ArgTypes...)>

It has

template<class F> function& operator=(F&& f);

with the following remark:

, decay_t <F> Lvalue-Callable ArgTypes... R.

F Lvalue-Callable ArgTypes R, INVOKE <R> (declval <F> (), declval <ArgTypes> ()...), , .

, std::function<R(ArgTypes...)> , ArgTypes..., - R.

, , std::function .

+5

All Articles