Template Struct Generator

Nested Relationships

Nested Types

Struct Documentation

template<typename T>
struct Generator

Coroutine-based generator for lazy iteration.

Generator provides a C++20 coroutine-based iterator that yields values on demand. Used for streaming operations where producing all values upfront would be inefficient.

Supports both functional-style iteration via operator() and C++ iterator protocol for use with range-based for loops and standard algorithms.

Template Parameters:

T – Type of values yielded by the generator.

Public Types

using handle_type = std::coroutine_handle<promise_type>

Public Functions

inline explicit Generator(handle_type h)

Construct generator from coroutine handle.

Generator(const Generator&) = delete

Deleted copy constructor.

Generator &operator=(const Generator&) = delete

Deleted copy assignment operator.

inline Generator(Generator &&other) noexcept

Move constructor - transfers ownership and nulls source handle.

inline Generator &operator=(Generator &&other) noexcept

Move assignment operator - transfers ownership and nulls source handle.

inline ~Generator()

Destructor destroys the coroutine handle if valid.

inline iterator begin()

Get iterator to the beginning of the generator. Advances to the first value.

inline std::default_sentinel_t end()

Get sentinel marking the end of the generator.

inline explicit operator bool()

Check if more values are available.

Returns:

true if generator has more values, false otherwise.

inline T operator()()

Get the next value from the generator.

Throws:

std::runtime_error – if generator has been moved from.

Returns:

Next yielded value.

Public Members

handle_type h_
struct iterator

Iterator for C++ range-based for loop support.

Public Types

using iterator_category = std::input_iterator_tag
using difference_type = std::ptrdiff_t
using value_type = T
using pointer = T*
using reference = T&

Public Functions

inline iterator &operator++()
inline T &operator*() const
inline bool operator==(std::default_sentinel_t) const

Public Members

handle_type h_
struct promise_type

Coroutine promise implementation.

Public Functions

inline Generator get_return_object()

Create generator from promise.

inline std::suspend_always initial_suspend()

Suspend at coroutine start.

inline std::suspend_always final_suspend() noexcept

Suspend at coroutine end.

inline void unhandled_exception()

Handle unhandled exceptions.

template<std::convertible_to<T> From>
inline std::suspend_always yield_value(From &&from)

Yield a value from the coroutine.

inline void return_void()

Complete coroutine without returning a value.

Public Members

T value

Current yielded value.

std::exception_ptr exception

Exception captured during generation.

bool full = false

Whether the current value has been filled but not yet consumed.