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