C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::any::base

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 19 Apr 2024 11:11:19 +0100
On Wed, Apr 17, 2024 at 4:13 PM Thiago Macieira wrote:
>
> I believe we've already concluded there's no ABI break necessary
> and the vendor is the ultimate authority anyway.


I think it might not be possible without an ABI break on Microsoft.

It turns out that the source code for "__RTDynamicCast" is available here:

    https://github.com/ojdkbuild/tools_toolchain_vs2017bt_1416/blob/master/VC/Tools/MSVC/14.16.27023/crt/src/vcruntime/rtti.cpp#L205

and it invokes a function called "FindVITargetTypeInstance":

    https://github.com/ojdkbuild/tools_toolchain_vs2017bt_1416/blob/master/VC/Tools/MSVC/14.16.27023/crt/src/vcruntime/rtti.cpp#L695

And after looking through the code, it looks like it will only work if
the source object is polymorphic. "FindVITargetTypeInstance" needs
access to the "RTTICompleteObjectLocator" for the most-derived object
-- but non-polymorphic types don't have one. Also, the
"std::type_info" doesn't contain a flag to say whether or not the type
is polymorphic.

If we forget about the "dynamic_cast" way of doing it, and instead
focus on the "throw an exception" way of implementing it, then we need
access to the type's "ThrowInfo" -- but the compiler will only emit
the "ThrowInfo" if the type is thrown. If the type is never thrown,
there's no "ThrowInfo". And even if we could guarantee the presence of
the "ThrowInfo" in the DLL or EXE file, we would need to figure out a
way of getting the ThrowInfo from the type_info -- I don't see an
obvious link.

So if you have an "std::any" object, and if you assign a
non-polymorphic type to it, and then try to get a pointer to a base
class, there's no way of making it work -- not without adding more
information to "std::any" and therefore inducing an ABI break.

What we _could_ implement without an ABI break is a member function
called "base_from_polymorphic", which you would only use if you're
certain that the contained value is a polymorphic object.

By the way I've also looked through Microsoft's <any> header and I
don't see anything in there that would make this possible. Of course
the guys at Microsoft might know something that I don't know.

Received on 2024-04-19 10:11:31