C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sat, 13 Apr 2024 01:54:23 +0200
sob., 13 kwi 2024 o 01:06 Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Fri, Apr 12, 2024 at 8:31 PM Marcin Jaczewski wrote
> >
> > This should be possible to implement today, performance will be bad as it abuse
> > `throw` and `catch`:
>
>
> I implemented it here last week:
> https://lists.isocpp.org/std-proposals/2024/04/9500.php
>
> Implementing it on Microsoft is a little trickier as you need to
> somehow turn a type_info into a ThrowInfo . . I think I posted an
> implementation of that here earlier this week too.
>
> But since the exception-throwing system just takes a look through a
> list of type_info's of base types, you can just look through those
> type_info's yourself instead of throwing an exception.

Is this your version of portable C++? no.
Is this code even correct? no.
Does ThrowInfo or similar data exist for every type? no.
This is even good code? NO.

This means you do not implement this, you create code full of UB that
only by chance works somehow.

If you are convinced that your idea is good, your are free to use
this code in your production environment but please do not
waste anyone else's time on "solutions" like this.


Backing to meritum of whole problem, if someone need
base from `std::any` simplest solution is use custom type like:

```
using ThrowCastFunc = void(*)(std::any&);

struct MyAny : std::any
{
    ThrowCastFunc throw_object_ptr;

    template<typename T>
    T* get_base()
    {
        try
        {
            throw_object_ptr(*this);
        }
        catch (T* b)
        {
           return b;
        }
        catch (...)
        {
           return nullptr;
        }
    }

    template<typename T>
    MyAny(T&& t) : std::any(t), throw_object_ptr { +[](std::any& a) {
throw std::any_cast<T>(&a); } } {}
}
```

Simple and portable and works in C++17.

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-04-12 23:54:37