C++ Logo

std-proposals

Advanced search

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

From: Zhihao Yuan <zy_at_[hidden]>
Date: Thu, 15 Sep 2022 20:32:50 +0000
On Wednesday, September 14th, 2022 at 2:57 PM, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:


> On Wed, Sep 14, 2022 at 8:15 PM Lénárd Szolnoki via Std-Proposals
> std-proposals_at_[hidden] wrote:
>

> > There is no reason that variant<Cat, Dog> and variant<specify_base<Animal>, Cat, Dog> should be different types.
>

>

> The former doesn't allow the variant object to have no object. The
> second one is very similar to:
>

> variant< monostate, Cat, Dog >
>


I agree that variant<monostate, Cat, Dog>
can be a model for
variant<specify_base<Animal>, Cat, Dog>,
but I also want variant<Cat, Dog> to
implement an Animal interface.
To summarize, I think

  nonnull
  nullable
  throwing

All make sense.

How about this:

  template<class T>
  struct implements
  {
    template<class... Ts>
    using model = std::variant<Ts...>;
  };

  template<class T>
  struct implements_nullable
  {
    template<class... Ts>
    using model = std::variant<std::monostate, Ts...>;

    static auto default_proxy() noexcept { return nullptr; }
  };

  template<class T>
  struct implements_or_throw
  {
    template<class... Ts>
    using model = std::variant<std::monostate, Ts...>;
  };

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

Received on 2022-09-15 20:33:02