C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Fri, 12 Apr 2024 21:31:28 +0200
pt., 12 kwi 2024 o 18:31 Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Friday 12 April 2024 08:56:09 GMT-7 Frederick Virchanza Gotham via Std-
> Proposals wrote:
> > 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.
> > }
>
> Remove this. This is a means-to-an-end, not the objective.
>
> Your paper should be only about std::any. It should not talk about *how* to
> implement the functionality. It may want to list your investigations that the
> information is available already and thus should be implementable without
> breaking ABI.
>

This should be possible to implement today, performance will be bad as it abuse
`throw` and `catch`:


```
struct AA
{

};

struct BA: AA
{

};

using ThrowCastFunc = void(*)(void*);

template<typename T>
T* catch_base_ptr(ThrowCastFunc throw_cast, void* obj)
{
    try
    {
        throw_cast(obj);
    }
    catch (AA* b)
    {
        return b;
    }
    catch (...)
    {
        return nullptr;
    }
}

template<typename T>
auto throw_object_ptr(void* obj)
{
    throw static_cast<T*>(obj);
}

int main()
{
    auto ptr = new BA();
    ThrowCastFunc func = throw_object_ptr<BA>;
    auto base = catch_base_ptr<AA>(func, ptr);

    assert(base == static_cast<AA*>(ptr));
}
```


As we see, `catch_base_ptr` does not need to know `throw_object_ptr`,
only the problem
that `any` does not have any place to add the `throw_object_ptr`
function without
breaking ABI.

Porobaby best solution would be to add this to `std::polymorphic_value`
from the proposal that Ville mentioned.
Effective `std::polymorphic_value<void>` would work as `std::any` but more heavy
as allow dynamic casts to arbitrary base objects.


> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel DCAI Cloud Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-04-12 19:31:41