C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow conversion of memfunc pointers to func pointers

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 14 Jan 2024 17:57:31 -0800
On Sunday, 14 January 2024 15:27:56 PST Frederick Virchanza Gotham via Std-
Proposals wrote:
> It's easy to get the address of any class's vtable if you have an
> object of the class, for example with the following function:
>
> template<typename T>
> void (*GetVtable(T const &arg))(void)
> {
> static T obj;
> return *static_cast<void (*const *)(void)>(static_cast<void
> const *>(&arg));
> }

The pointer to the vtable does not have to be at the top of the object.

> But I have another idea. The mangled linker symbol for the vtable for
> any class is: _ZTV5Name

No, it's _ZTV4Name.

> So if the class is called 'Donkey', then the vtable linker symbol is
> '_ZTV5Donkey'.

And this one is _ZTV6Donkey.

>
> extern void (*const _ZTV5##Class)(void);
> \
> void (*const *const &vtable)(void) = &_ZTV5##Class;

This only works for classes whose names are 5 characters long and in the
global namespace. For classes in other namespaces, you need the N...E
construct, except if the namespace is std.

Then there's such a think as mangling of the template instantiations in I...E
too, if this class is such.

> And while I'm talking . . . maybe it would be worth mentioning that
> name mangling should be standardised too?

I agree, but adopting the mangling scheme would be useless without adopting
the rest of the ABI in the first place. There's one compiler vendor that seems
to have little interest in being compatible with everyone else. That one also
has a number of defects in its mangling scheme causing non-conformity with the
standard in a few areas, so it really ought to adopt it.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2024-01-15 01:57:33