2 ms·
That's what c++20 coroutines paired with c++23 generators are for. It's easy to define whatever traversal you want and then expose it as generic code. https://
by injidup 1y ago
That's what c++20 coroutines paired with c++23 generators are for. It's easy to define whatever traversal you want and then expose it as generic code.
https://godbolt.org/z/fnGzszf3j https://godbolt.org/z/fnGzszf3j
- ellis0n 1y agoGreat example!
- uecker 1y agoYeah, but is this really better than explicitly managing a stack? Here is how I one could write in C: https://godbolt.org/z/P3ErP4T4d https://godbolt.org/z/P3ErP4T4d or possibly with the setup code moved into the helper function for convenience: https://godbolt.org/z/soz7W5z1G https://godbolt.org/z/soz7W5z1G
- injidup 1y agoSure you can do it any way but the OP was about adding language features for iteration of a specific data structure. Iteration in C++ is state machine based and coroutines give you a neat way to implicitly model them without having to manage the boiler plate yourself.
- uecker 1y agoYeah, but what I do not get is why you need all this. What problem does this solve? The C function that advances to the next tree node does exactly the same. And what is neat about it? It just seems a lot more complicated. Or in other words: A new language feature should make things simpler and not more complicated to express. But maybe I am just missing a realistic examples where it actually makes things simpler?