I am looking into the ranges library of C++20 and I was wondering how easy it might be to build a range adaptor from a function template<Iterator I,Iterator J> I f(J) (or maybe a more constrained version of it). The particular example I had in mind would be the concurrent evaluation of a stream of std::function<I()>s. The function I had in mind might be something like:
// use std::async to concurrently evaluate up to 10 functions from the input iterator.
template<typename T, typename It>
generator<T> async_eval(It it) {
std::queue<std::future<T>> queue;
for (auto& i : it) {
// Wait for a slot to free.
while (queue.size() >= 10) {
co_yield queue.front().get();
queue.pop();
}
// Now there is space in the queue, we can safely push to it.
queue.emplace(std::async(i));
}
// Empty the queue.
while (queue.size() >= 0) {
co_yield queue.front().get();
queue.pop();
}
}
Then I would like an as_adaptor function so that I can say ... | transform([](int x) { return [x](){return x+1};}) | as_adaptor(async_eval) | .... Is this, or anything similar, possible?