admin管理员组文章数量:1393085
In C#, I can write the following code:
IEnumerable<int> evenUpTo(int max)
{
for (int i = 0; i < max; i += 2)
{
yield return i;
}
}
foreach(int e in evenUpTo(11))
{
Console.WriteLine(e);
}
And the compiler will generate a class for me, which satisfies the IEnumerable<int>
interface.
Now, C++20 got coroutines as well. In principle, the code looks very similar:
#include <print>
#include <coroutine>
??? evenUpTo(int max) {
for (int i = 0; i < max; i += 2) {
co_yield i;
}
}
int main() {
for (auto e : evenUpTo(11)) {
std::print("{0}\n", e);
}
}
What do I put as the return type ???
?
I read that IEnumerable isn't needed in C++, because C++ has iterators. A pair of iterators (.begin()
and .end()
) is certainly enough to get a range-based for-loop working. But which container would I use? But linked question is from 2012 and coroutines are from C++20, so this topic hasn't been covered.
I can probably write my own Generator<T>
template, but I'd like to avoid that, if there is a built-in STL type.
In C#, I can write the following code:
IEnumerable<int> evenUpTo(int max)
{
for (int i = 0; i < max; i += 2)
{
yield return i;
}
}
foreach(int e in evenUpTo(11))
{
Console.WriteLine(e);
}
And the compiler will generate a class for me, which satisfies the IEnumerable<int>
interface.
Now, C++20 got coroutines as well. In principle, the code looks very similar:
#include <print>
#include <coroutine>
??? evenUpTo(int max) {
for (int i = 0; i < max; i += 2) {
co_yield i;
}
}
int main() {
for (auto e : evenUpTo(11)) {
std::print("{0}\n", e);
}
}
What do I put as the return type ???
?
I read that IEnumerable isn't needed in C++, because C++ has iterators. A pair of iterators (.begin()
and .end()
) is certainly enough to get a range-based for-loop working. But which container would I use? But linked question is from 2012 and coroutines are from C++20, so this topic hasn't been covered.
I can probably write my own Generator<T>
template, but I'd like to avoid that, if there is a built-in STL type.
1 Answer
Reset to default 8C++23
C++23 now has the std::generator<>
template, which you get with #include <generator>
.
#include <print>
#include <coroutine>
#include <generator>
std::generator<int> evenUpTo(int max) {
for (int i = 0; i < max; i += 2) {
co_yield i;
}
}
int main() {
for (auto e : evenUpTo(11)) {
std::print("{0}\n", e);
}
}
Compiler Explorer: https://godbolt./z/s66jbq9c7
C++20
Reiner Grimm explains how to write your own template in the book Concurrency with Modern C++ [Amazon]. There's an updated version by Roger Voss available on Github.
There's also a the reference implementation [Github] of the proposal P2168 which suggests to introduce the synchronous std::generator
to C++23.
版权声明:本文标题:c++20 - What do I use in C++ instead of IEnumerable<T> for the return type of a co_yield function? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744605697a2615339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论