C++ Logo


Advanced search

[std-proposals] Do not accept std::generator, please.

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Sat, 3 Sep 2022 13:49:06 +0500
std::generator has huge problems.
I will briefly list them:
1. The generator must not depend on the allocator (only the promise_type
depends), but std::generator has a template argument for alloc, in
particular, this prohibits adding 2 generators that allocated memory
differently into a container. It is possible to implement this without type
Link to example implementation:

2. Overhead for ueslss default type erasure(default behavior) and stack in
generator (used only for element_of, which is basically for(auto&& v : gen)
co_yield v;
Its just uselss
You pay for what you dont use.
3. It is undefined behavior to call begin for std::generator twice. But
iteraing not all values in one loop is a very logical action and expected
behavior - just keep generating values
Besides, such an error is extremely non-obvious when using ranges or range
based for loop.
4. interface that is very obscure to the user and requires knowledge of
implementation to write efficient code
No one will write generator<const T&> foo(); without knowing why it is
needed and why it is better for perfmornace

But there are way to write generator without such problems

Received on 2022-09-03 08:49:15