из https://en.cppreference.com/w/cpp/corou ... rator.html: написано.
Код: Выделить всё
#include
#include
template
struct Tree
{
T value;
Tree *left{}, *right{};
std::generator traverse_inorder() const
{
if (left)
co_yield std::ranges::elements_of(left->traverse_inorder());
co_yield value;
if (right)
co_yield std::ranges::elements_of(right->traverse_inorder());
}
};
int main()
{
Tree tree[]
{
{'D', tree + 1, tree + 2},
// │
// ┌───────────────┴────────────────┐
// │ │
{'B', tree + 3, tree + 4}, {'F', tree + 5, tree + 6},
// │ │
// ┌─────────┴─────────────┐ ┌───────────┴─────────────┐
// │ │ │ │
{'A'}, {'C'}, {'E'}, {'G'}
};
for (char x : tree->traverse_inorder())
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79703343/compiling-stdgenerator-with-optimization-produces-null-dereference-in-gcc[/url]
Мобильная версия