我正在研究 C++20 的范围库,我想知道从一个函数template<Iterator I,Iterator J> I f(J)
(或者可能是它的一个更受限制的版本)构建一个范围适配器有多容易。我想到的特定示例是对std::function<I()>
s 流的并发评估。我想到的功能可能是这样的:
// 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();
}
}
然后我想要一个as_adaptor
函数,以便我可以说... | transform([](int x) { return [x](){return x+1};}) | as_adaptor(async_eval) | ...
。这是可能的吗?