C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Chimeric Pointer

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 25 Nov 2022 13:35:35 +0000
On Thu, Nov 24, 2022 at 11:41 PM Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:

> Instead of
>
> void Red( chimeric_pointer<wxControl,wxTextEntry> p )
<snip>
> Write:
> void Red(std::Variant<wxControl,wxTextEntry> &p )
<snip>

The closest I can get to what you're describing is the following code:

#include <string>
#include <variant>
#include <exception>

class Monkey : virtual public std::exception, virtual public std::string {};
class Donkey : virtual public std::string , virtual public std::exception {};

void Red( std::variant<std::string,std::exception> &p )
{
    std::visit([](auto &p) {
        if constexpr (requires { p.what(); }) p.what();
        if constexpr (requires { p.size(); }) p.size();
    }, p);
}

void Func(void)
{
    Monkey m;
    Donkey d;

    Red(m);
    Red(d);
}

Note however that this doesn't compile, because the object called 'm'
can't be converted from 'Monkey &' to 'variant<string,exception> &'.

What exactly did you have in mind? I don't see how you were expecting
that to work.

Received on 2022-11-25 13:35:48