Inspired by this comment about binding lambdas with rvalue link parameters directly to std::async , binding rvalue to lambda via std::async compiles and runs as expected: ( live example )
auto lambda = [] (std::string&& message) { std::cout << message << std::endl; }; auto future = std::async(lambda, std::string{"hello world"}); future.get();
Using std::bind , however, raises a compiler error: ( live example )
auto lambda = [] (std::string&& message) { std::cout << message << std::endl; }; auto bound = std::bind(lambda, std::string{"hello world"});
This is because std::bind saves the message as an lvalue, so when it passes it to lambda, the argument no longer matches the parameter.
I read that std::async internally uses std::bind since it leaves with the rvalue link parameters when std::bind not? Is there a certain part of the standard that requires this behavior, or does it depend on the compiler?
c ++ lambda c ++ 11 rvalue-reference stdbind
huu
source share