Is std::async supose to work with template function? I\'ve tried to lauch std::reverse as an asynchronous task bu got compile-time error.
I\'ve tried to use simpler
std::reverse
is not a function but a function template, you can use an specialization of that template (which is a function):
auto result_reverse = std::async(&std::reverse<std::string::iterator>, str.begin(), str.end());
It can, but the invocation is a bit different:
auto result_reverse = std::async([&str]() {
std::reverse(str.begin(), str.end());
});
This is because std::reverse()
is not a function, rather a function template that turns into a function when it is invoked as a function.
The above reverses a copy of the string and throws away the result. To pass the string by reference the lambda expression should be changed to start with [&str]()
.