C++ Logo

std-proposals

Advanced search

Re: [std-proposals] [DRAFT PAPER] std::variant with std::specify_base

From: Zhihao Yuan <zy_at_[hidden]>
Date: Mon, 26 Sep 2022 19:22:39 +0000
On Sunday, September 25th, 2022 at 10:59 AM, Bengt Gustafsson via Std-Proposals <std-proposals_at_[hidden]> wrote:


> Instead of using an ill-suited class such as std::variant to store an instance of one of a set of subclasses and require std::variant to get a new method to handle this situation better I think that a more suitable way of storing a subclass instance would be inside a polymorphic_value with SBO buffer of sufficient size.
>

> [...]
>

> A type alias which handles a closed set of subclasses size and alignment needs called polymorphic_value_for<T, SubClasses...> is included in the latest version for added convenience. I'm unsure if this is important enough to be standardized though. Enumerating all the subclasses seems like such an anti-pattern.

polymorphic_value_for<T, SubClasses...>
looks like a closed set but is not.


std::variant dispatches on a closed set oftypes, and I see nothing wrong with
performing such dispatch with a compiler-
generated vtable. Doing so violates no type
system and delivers cleaner semantics than
a type that behaves as-if an unknown set
of types with size constraints.
polymorphic_value is a useful type, but the
claim that suggests "it suites better for your
purpose" is unproven.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
_______________________________________________

Received on 2022-09-26 19:22:48