mercredi 6 mai 2015

std::async using an rvalue reference bound to a lambda

I'm trying to bind an rvalue reference to a lambda using std::bind, but I have issues when I throw that into a std::async call: (source)

auto lambda = [] (std::string&& message) {
    std::cout << message << std::endl;
};
auto bound = std::bind(lambda, std::string{"hello world"});
auto future = std::async(bound); // Compiler error here
future.get()

This issues a compiler error I'm not really sure how to interpret:

error: no type named 'type' in 'class std::result_of(std::basic_string)>&()>'

What's going on here? Interestingly, a slight modification does compile and work as expected. If I change std::string{"hello world"} to a c-string literal, everything works fine: (source)

auto lambda = [] (std::string&& message) {
    std::cout << message << std::endl;
};
auto bound = std::bind(lambda, "hello world");
auto future = std::async(bound);
future.get(); // Prints "hello world" as expected

Why does this work but not the first example?

Aucun commentaire:

Enregistrer un commentaire