简体   繁体   中英

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())
}

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

Guys i have hacked up a perfect forwarding version of a binder(limited to 1 param) here http://code-slim-jim.blogspot.jp/2012/11/stdbind-not-compatable-with-stdmove.html

For reference the code is

template <typename P>
class MovableBinder1
{
  typedef void (*F)(P&&);

private:
  F func_;
  P p0_;

public:
  MovableBinder1(F func, P&& p) :
    func_(func),
    p0_(std::forward<P>(p))
  {
    std::cout << "Moved" << p0_ << "\n";
  }

  MovableBinder1(F func, P& p) :
    func_(func),
    p0_(p)
  {
    std::cout << "Copied" << p0_ << "\n";
  }

  ~MovableBinder1()
  {
    std::cout << "~MovableBinder1\n";
  }

  void operator()()
  {
    (*func_)(std::forward<P>(p0_));
  }
};

As u can see from the above proof of concept, its very possible...

I see no reason why std::bind is incompatible with std::move... std::forward is after all for perfect forwarding I dont understand why there isnt a std::forwarding_bind ???

One more improvement in GManNickG's answer and I've got pretty solution:

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

(works in gcc-4.9.2 and msvc2013)

(This is actually a comment to GMan's answer, but I need some formatting for the code). If generated functor actually is like this:

struct your_bind
{
    your_bind(Movable arg0) :
    arg0(arg0)
    {}

    void operator()()
    {
        foo(arg0);
    }

    Movable arg0;
};

then

int main()
{
    auto f = your_bind(Movable());
    f();    // No errors!
}

compliles without errors. as it's possible to assign and initialize data with rvalue and then pass a data value to rvalue argument of the foo().
However, I suppose that bind implementation extracts function argument type directly from foo() signature. ie the generated functor is:

struct your_bind
{
    your_bind(Movable && arg0) :
    arg0(arg0) // ****  Error:cannot convert from Movable to Movable &&
    {}

    void operator()()
    {
        foo(arg0); 
    }

    Movable&& arg0;
};

and indeed, this really fails to initialize rvalue data member. Perhaps,the bind implpementation simply does not correctly extract "unreferenced" type from function argument type and uses this type for functor's data member declaration "as is", without trimming &&.

the correct functor should be:

struct your_bind
{
    your_bind(Movable&& arg0) :
    arg0(arg0)
    {}

    void operator()()
    {
        foo(arg0); 
    }

    Movable arg0; // trim && !!!
};

You could use a lambda expression.

auto f = [](){ foo(Movable()); };

This would seem to be the simplest option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM