C++ Logo

std-proposals

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
erasure.
Link to example implementation:
https://github.com/kelbon/dd_generator/blob/4053fcff456c885d9330708d2e67f103c4212319/include/dd_generator.hpp#L264

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
https://github.com/kelbon/dd_generator

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