为什么下面的代码不能编译?
#include <future>
#include <numeric>
#include <vector>
int main() {
const std::vector<int> values{1, 2, 3, 4, 5};
auto futures = std::accumulate(
begin(values), end(values), std::vector<std::future<int>>{},
[](auto accumulator, auto value) {
accumulator.push_back(
async(std::launch::async, [value] { return value; }));
return accumulator;
});
}
https://cpp.godbolt.org/z/c967ra1hY
有趣的是,使用 cppreference 的 accumulate 实现 https://cpp.godbolt.org/z/MdM8Wh3Eq.