如何解决C++20 范围库中到范围适配器的迭代器转换器
我正在研究 C++20 的范围库,我想知道从函数 template<Iterator I,Iterator J> I f(J)
(或者可能是它的更受限版本)构建范围适配器有多么容易。我想到的特定示例是对 std::function<I()>
流的并发评估。我想到的功能可能是这样的:
// 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) | ...
。这或任何类似的可能吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。