This question is specifically about non-portable MSVC ABI stuff.
I'm trying to write the equivalent of C++'s typeid
in obviously-nonportable-yet-not-magic C++. For the Itanium ABI (as used on Linux/Mac), it's super easy:
const std::type_info& dynamicast_typeid(void *mdo)
{
std::type_info **vptr = *reinterpret_cast<std::type_info ***>(mdo);
std::type_info *typeinfo_ptr = vptr[-1];
return *typeinfo_ptr;
}
So now I'm looking at the 64-bit MSVC ABI, and dang it, I'm stumped. For very simple classes, the ones that start with a vfptr at offset 0, it's almost as easy as Itanium:
const std::type_info& dynamicast_typeid(void *mdo)
{
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *) rtti_complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}
(This code is based on the Wine project's __RTtypeid
.)
The problem is that some C++ classes don't start with a vfptr at offset 0! Sometimes they start with a vbptr.
struct Class1 { virtual ~Class1() {} };
struct Class2 : virtual Class1 {};
Class1
starts with a vfptr; Class2
starts with a vbptr.
When a class starts with a vbptr, I believe it's always the case that its first virtual base subobject (its first in layout order, and thus its "leafiest") will always have a vfptr at its offset 0. So if I know I'm dealing with a class that starts with a vbptr, I want to do this instead:
const std::type_info& dynamicast_typeid_for_vbptr_class(void *mdo)
{
int first_vbase_offset = ((int**)mdo)[0][1];
mdo = (char*)mdo + first_vbase_offset;
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *) rtti_complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}
I've determined that MSVC does actually generate the equivalent of
if constexpr(IS_VBPTR_CLASS) {
int first_vbase_offset = ((int**)mdo)[0][1];
mdo = (char*)mdo + first_vbase_offset;
}
return __RTtypeid(mdo);
when compiling the C++ expression typeid(x)
— where that IS_VBPTR_CLASS
is pseudocode for "the compiler magically knows whether x
has a vbptr, based on the static type of x
and the fact that the compiler knows the layout of every type."
However, in my case I don't know the static type of x
, and even if I did, I don't know how to find out (from within C++, with template metaprogramming) whether x
starts with a vbptr or not.
Finally, I went ahead and fudged it a little bit with
const std::type_info& dynamicast_typeid(void *mdo)
{
while (((int**)mdo)[0][0] == 0) {
mdo = (char *)mdo + ((int**)mdo)[0][1];
}
int *rtti_complete_object_locator = ((int ***)mdo)[0][-1];
char *result = (char *)complete_object_locator;
result -= rtti_complete_object_locator[5];
result += rtti_complete_object_locator[3];
return *(std::type_info*)result;
}
only to discover that the typeinfo stored in the vftable for "Class1 in Class2" holds the typeinfo for the subobject type Class1
, not for the most-derived-type Class2
! So there's another piece of the puzzle missing.
So my question in a nutshell: Given (void*)&object_of_type_class2
, how do I retrieve typeid(Class2)
?
void*
doesn't carry any type information. You need at least areinterpret_cast
. – Barnette