Question

I would like to create a std::function bound to a member function taking a rvalue argument. Here is my attempt which does not compile ("xxfunction(154): error C2664: ... You cannot bind an lvalue to an rvalue reference" among others).

    class C
    {
    public:
        void F(std::string &&s)
        {
                //do something with s
        }
        C(){}
    };
    C c;
    std::function<void(std::string&&)> pF = std::bind(&C::F,&c,std::placeholders::_1);
    //somewhere far far away
    pF(std::string("test"));

I did some reading and I think it has to do with std::function no using perfect forwarding but I cant figure out how to get it to work.

Edit:

std::function<void(std::string&)> pF = [&c](std::string&in){c.F(std::move(in));};

This is a semi viable work around but it is not perfect because calling pF will work with an lvalue now and move it anyway.

std::string s("test");
pF(s);
auto other = s; //oops
Était-ce utile?

La solution

It seems your std::bind implementation does not support perfect forwarding via placeholders, but since you have C++11 and std::bind is ugly anyways, use lambdas:

std::function<void(std::string&&)> pF 
  = [&c](std::string&& str)
{ 
  c.F(std::move(str)); 
};

Edit:

Note: Although accepted, this answer is not a solution to the problem at hand, because it is not std::bind but MSVC10's std::function implementation that is flawed. However, the suggestion has led to a workaround and therefore was accepted by PorkyBrain.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top