C++ Logo


Advanced search

Re: [std-proposals] Get base class from std::any

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Wed, 29 Mar 2023 21:35:02 +0500
> So the question is whether it would be possible in all implementations to
> add any_base_cast / any_dynamic_cast without ABI break. We know that it is
> in libstdc++ and libc++. Do we have an answer for MSVC-STL?

I was playing with it smth. like 5-6 years ago and have lost the code, but I'll try to write what I remember.

Unlike Itanium ABI, in MSVC, exception handling is not based (only) on type_info, and the corresponding structures are not easily obtainable.

AFAIR, I had a template function throwing an exception of type T*:

template<typename T>
void throwing() { throw T*{}; }

I was obtaining a pointer to it (`&throwing<T>`) and then scanned the function body binary code to get the address of the corresponding exception type info (it is dumped to the object file, but there is no symbol referring to it). AFAIR, it is a structure of ThrowInfo type [0], which has pCatchableTypeArray member [1], which enumerates the types of the handlers which can capture the exception.

So, when storing an object of type D, one should save its ThrowInfo, and then search for the B's CatchableType [2] when trying to cast to B, and, if present, it contains the offset to the B subobject [3].
AFAIR, pType [4] is just std::type_info.

So this method is ISA-dependent, because one will have to find the instruction containing the address (offset to) the ThrowInfo in `throwing<T>` binary code. Maybe ThrowInfo is more easily obtainable (now).

[0] https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L369-L384
[1] https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L382
[2] https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L286-L298
[3] https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L293
[4] https://github.com/Chuyu-Team/VC-LTL/blob/master/src/14.0.24210/ehdata.h#L288-L292

Received on 2023-03-29 16:35:09