C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 15 Sep 2022 22:57:46 +0100
On Thu, Sep 15, 2022 at 9:33 PM Zhihao Yuan <zy_at_[hidden]> wrote:
>
> 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...>;
> };


It isn't immediately apparent to me how you would use those. Something
like the following?

    implements<Animal>::model<Dog,Cat,Fish> obj;

    obj->Eat();

The whole point of my proposal is to have this simple syntax:
obj->BaseClassMethod();

I want it to all be inside "std::variant" so that the object can be
used in templates that expect an std::variant, and also so that the
"std::variant" object can be copied and it will still retain all of
its info (which wouldn't happen if we made a new class derived from
"std::variant").

Received on 2022-09-15 21:57:58