Is std::move
necessary in the following snippet?
std::function<void(int)> my_std_function;
void call(std::function<void(int)>&& other_function)
{
my_std_function.swap(std::move(other_function));
}
As far as I know call()
accepts a rvalue reference.. but since the rvalue reference is itself an lvalue, in order to call swap(std::function<void(int)>&&)
I have to re-cast this to an rvalue reference with std::move
Is my reasoning correct or std::move
can be omitted in this case (and if it can, why?)
std::function::swap
does not take its parameter by rvalue reference. It's just a regular non-const
lvalue reference. So std::move
is unhelpful (and probably shouldn't compile, since rvalue references aren't allowed to bind to non-const
lvalue references).
other_function
also doesn't need to be an rvalue reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With