[cfe-dev] RTTI for virtual inheritance (without virtual functions)

Roman Popov via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 14 12:03:25 PDT 2018


Hello,
I've noticed that Clang generates typeinfo for non-polymorphic types with
virtual inheritance. Why is that?

My understanding is that RTTI should not be provided for non-polymorphic
types.
Here is what I've read in ABI:

https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable

The *typeinfo pointer* points to the typeinfo object used for RTTI. It is
always present. All entries in each of the virtual tables for a given class
must point to the same typeinfo object. A correct implementation of
typeinfo equality is to check pointer equality, except for pointers
(directly or indirectly) to incomplete types. *The typeinfo pointer is a
valid pointer for polymorphic classes, i.e. those with virtual functions,
and is zero for non-polymorphic classes.*

So I've expected 0 value of typeinfo for classes without virtual functions.
 In practice however both GCC and Clang generate typeinfo. Why is that?

Example:

struct VirtualBase {};

struct Derived0 : virtual VirtualBase{};
struct Derived1 : virtual VirtualBase{};
struct MostDerived : Derived0, Derived1 {};

int main()
{
    MostDerived md;
    uintptr_t **vtable = (uintptr_t **)&md;
    type_info * tinfo = (type_info *)(*vtable)[-1];
    cout << tinfo->name() << "\n";
    return 0;
}

Output:
11MostDerived

Thanks,
Roman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180914/2d13ef93/attachment.html>


More information about the cfe-dev mailing list