Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing rvalues through std::bind

I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can't figure out how to do it. For example:

#include <utility> #include <functional>  template<class Type> void foo(Type &&value) {     Type new_object = std::forward<Type>(value);    // move-construct if possible }  class Movable { public:     Movable(Movable &&) = default;     Movable &operator=(Movable &&) = default; };  int main() {     auto f = std::bind(foo<Movable>, Movable());     f();    // error, but want the same effect as foo(Movable()) } 
like image 350
Timothy003 Avatar asked Feb 02 '11 06:02

Timothy003


People also ask

What does std :: bind do in C++?

std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

How does STD bind work?

std::bind allows you to create a std::function object that acts as a wrapper for the target function (or Callable object). std::bind also allows you to keep specific arguments at fixed values while leaving other arguments variable.

Is std :: bind deprecated?

Yes: std::bind should be replaced by lambda For almost all cases, std::bind should be replaced by a lambda expression. It's idiomatic, and results in better code.

What is the return type of std :: bind?

std::bind. Returns a function object based on fn , but with its arguments bound to args . Each argument may either be bound to a value or be a placeholder: - If bound to a value, calling the returned function object will always use that value as argument.


1 Answers

The reason this fails is because when you specify foo<Movable>, the function you're binding to is:

void foo(Movable&&) // *must* be an rvalue { } 

However, the value passed by std::bind will not be an rvalue, but an lvalue (stored as a member somewhere in the resulting bind functor). That, is the generated functor is akin to:

struct your_bind {     your_bind(Movable arg0) :     arg0(arg0)     {}      void operator()()     {         foo<int>(arg0); // lvalue!     }      Movable arg0; }; 

Constructed as your_bind(Movable()). So you can see this fails because Movable&& cannot bind to Movable.†

A simple solution might be this instead:

auto f = std::bind(foo<Movable&>, Movable()); 

Because now the function you're calling is:

void foo(Movable& /* conceptually, this was Movable& &&                         and collapsed to Movable& */) { } 

And the call works fine (and, of course, you could make that foo<const Movable&> if desired). But an interesting question is if we can get your original bind to work, and we can via:

auto f = std::bind(foo<Movable>,             std::bind(static_cast<Movable&&(&)(Movable&)>(std::move<Movable&>),                 Movable())); 

That is, we just std::move the argument before we make the call, so it can bind. But yikes, that's ugly. The cast is required because std::move is an overloaded function, so we have to specify which overload we want by casting to the desired type, eliminating the other options.

It actually wouldn't be so bad if std::move wasn't overloaded, as if we had something like:

Movable&& my_special_move(Movable& x) {     return std::move(x); }   auto f = std::bind(foo<Movable>, std::bind(my_special_move, Movable())); 

Which is much simpler. But unless you have such a function laying around, I think it's clear you probably just want to specify a more explicit template argument.


† This is different than calling the function without an explicit template argument, because explicitly specifying it removes the possibility for it to be deduced. (T&&, where T is a template parameter, can be deduced to anything, if you let it be.)

like image 125
GManNickG Avatar answered Oct 01 '22 21:10

GManNickG