What is std::generator (C++23) and how does it simplify coroutine use?
Short answer
std::generator (C++23) is a standard library coroutine return type for writing generators—so you can co_yield values without hand-writing a promise_type and iterator machinery.
Advertisement
AModel answer
std::generator (C++23) is a standard library coroutine return type for writing generators—so you can co_yield values without hand-writing a promise_type and iterator machinery. It provides a ready-made, range-compatible lazy sequence type, making the common generator use case simple and portable, unlike C++20 where you needed a custom or third-party type.
std::generator<int> squares(int n){
for (int i=0;i<n;++i) co_yield i*i;
}Real-world example
With std::generator, producing a lazy sequence needs only co_yield, no boilerplate coroutine plumbing.
Likely follow-ups
- What boilerplate does std::generator remove? | Is std::generator range-compatible?
Related topics