C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::typeid_except ( Paper Attached )

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 5 Mar 2024 09:34:02 +0000
On Sun, Mar 3, 2024 at 4:52 PM Arthur O'Dwyer wrote:
>
> On Sat, Mar 2, 2024 at 7:36 PM Frederick Virchanza Gotham wrote:
>
>> When it comes to logging exceptions when debugging (or even in release
>> builds), I would like if we could get a raw pointer to the exception
>> object along with the size of the exception object, so that we can
>> print its hex bytes
>
> You can get a raw pointer to the first byte of the most-derived object via
> `dynamic_cast<void*>(&pluginEx)`. Unfortunately I don't think you can
> get the size of the most-derived object in any way; all you know for sure
> is that it's no smaller than sizeof(pluginEx).


You've totally lost me here. 'dynamic_cast' to void* is typically used
to go from a base class to a most-derived class, for example:

    class Base1 { int a, b, c; };
    class Base2 { float x, y, z; virtual ~Base2(void){} };
    class Derived : public Base1, public Base2 { double k; };

    Derived obj;
    Base2 *pb = &obj;
    void *pd = dynamic_cast<void*>(pb);
    static_assert( (void*)&obj != (void*)pb );
    static_assert( (void*)&obj == (void*)pd );

But if you have an exception_ptr, something like:

    class std::exception_ptr {
        void *p;
        unsigned *ref_count;
    };

or:

    class std::exception_ptr : private std::shared_ptr<void> {
    };

Then why would you use dynamic_cast on it? Firstly it's not guaranteed
to be polymorphic, and secondly the exception object is not guaranteed
to be of a class type derived from exception_ptr. Am I missing
something here?

Currently in my own code, if I ever need to get the address of the
current exception object, I do:

    void *p = *(void**)&std::current_exception();

This of course isn't guaranteed to work even though it works on every
C++ compiler that I know of. Should we have a function in the standard
library especially for this purpose, such as:

    void *addressof_exception_object( std::exception_ptr const &p ) noexcept;

But since we're talking so much about exception_ptr lately, maybe
exception_ptr should be defined in the standard as follows:

    class exception_ptr {
        void *p;
        unsigned *ref_count;
        . . .
    public:
        type_info const &type(void) const noexcept;
        void *object_address(void) const noexcept;
        . . .
    };

Received on 2024-03-05 09:34:14