Is there a reference_wrapper<> for rvalue references?

前端 未结 4 1761
不思量自难忘°
不思量自难忘° 2020-11-30 10:43

I wonder how the following can be done

void f(string &&s) { 
  std::string i(move(s)); 
  /* other stuff */ 
} 

int main() { 
  std::string s; 
  bi         


        
4条回答
  •  长情又很酷
    2020-11-30 10:49

    How can I pass an rvalue reference and store it as an rvalue reference in the call wrapper?

    The problem here is that such a bind function object can be invoked multiple times. If the function object forwarded a bound parameter as rvalue this would obviously only work once. So, this is a bit of a safety issue.

    But in some cases this kind of forwarding is exactly what you want. You could use a lambda as an intermediary:

    bind([](string& s){f(move(s));},move(s));
    

    Basically, I came up with this bind+lambda combination as a workaround for a missing "move-capture".

提交回复
热议问题