Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `std::move` necessary here?

Tags:

c++

c++11

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?)

like image 441
Dean Avatar asked Jan 04 '16 14:01

Dean


1 Answers

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.

like image 152
Nicol Bolas Avatar answered Sep 16 '22 14:09

Nicol Bolas