coro/genmod.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <print>
#include <vector>
#include <generator>

std::generator<int&> genMod(auto& coll)
{
  for (int& elem : coll) {
    std::println("\tSUSPEND with {}", elem);    
    co_yield elem;
    std::println("\t RESUME with {}", elem);
  }
}

int main()
{
  std::vector<int> vec{0, 8, 15};
  std::println("vec: {}", vec);

  for (auto& val : genMod(vec)) {
    std::println(" main(): MODIFY {}", val);
    ++val;
  }
  std::println("vec: {}", vec);
}