C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extend std::type_info with more information

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 12 Apr 2024 16:56:09 +0100
On Fri, Apr 12, 2024 at 3:34 PM Mihail Mihaylov wrote:
>
> Scratch that, it turns out that `any_cast<A>(any*)` returns
> an `A*` to the value inside, which makes it meaningful to
> request the ability for this overload to return a non-null `A*`
> when the `any` contains a `B` where `B` is derived from `A`.


How about I change my paper so that it proposes two things.

Here's the first thing I propose, it's a standalone function:

    template<class Base> requires is_class_v<Base>
    Base *base(void *const p, type_info const &ti) noexcept
    {
        // returns a valid pointer to the base class
        // with the 'this' pointer adjusted if necessary,
        // or returns nullptr if 'Base' isn't a base.
    }

And here's the second thing I propose, it's a new member function for
'std::any':

    template<class Base> requires is_class_v<Base>
    Base *std::any::base(void) noexcept
    {
        // returns a valid pointer to the base class
        // with the 'this' pointer adjusted if necessary,
        // or returns nullptr if 'Base' isn't a base.

        // Possible implementation:
            return std::base<Base>( this->p, &this->ti );
    }

How about that?

Received on 2024-04-12 15:56:22