[cfe-dev] Fwd: Can't get address of member-function
Richard Smith
richard at metafoo.co.uk
Sun May 19 13:47:16 PDT 2013
On Sun, May 19, 2013 at 1:21 PM, salvatore benedetto <
salvatore.benedetto at gmail.com> wrote:
> On Sun, May 19, 2013 at 2:12 PM, Reid Kleckner <rnk at google.com> wrote:
> > Member function pointers are two pointers wide on Itanium. They look
> like
> > this:
> > struct {
> > union {
> > void *NonVirtualFunctionPointer;
> > ptrdiff_t VTableIndex;
> > };
> > ptrdiff_t ThisAdjustment;
> > };
> >
> > It's not surprising the reinterpret_cast fails. It seems gcc will try to
> > give you the function pointer part, and hope that you ultimately apply
> it to
> > an object that doesn't require an adjustment.
> >
>
> Under the assumption that I know what I am doing with that pointer, that is
> no virtual members and no multiple inheritance, would it be wrong to have
> clang return the NonVirtualFunctionPointer like GCC, along with a warning
> at most?
>
It would be a language extension, so you would need to justify the cost of
including it in Clang. I believe the GCC extension you want is the one
documented here:
http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Bound-member-functions.html#Bound-member-functions
> > On Sun, May 19, 2013 at 5:31 AM, salvatore benedetto
> > <salvatore.benedetto at gmail.com> wrote:
> >>
> >> I've sent this to cfe-users but perhaps it was better to send
> >> it here.
> >>
> >> S.
> >>
> >>
> >> ---------- Forwarded message ----------
> >> From: salvatore benedetto <salvatore.benedetto at gmail.com>
> >> Date: Sun, May 19, 2013 at 10:57 AM
> >> Subject: Can't get address of member-function
> >> To: cfe-users at cs.uiuc.edu
> >>
> >>
> >> Hi there,
> >>
> >> first of all, I know the following should not be done usually, but I'm
> >> working
> >> with C++ on a bare-metal project and it seems not to be forbidden by
> >> the standard.
> >>
> >> I need to get the address of a non-static member function and assign
> >> it to an integer variable.
> >>
> >> class MyClass
> >> {
> >> public:
> >> MyClass()
> >> {
> >> //someAddress = (uint32_t)&run; // This works OK with static
> >> member function
> >> someAddress = (uint32_t)reinterpret_cast<void
> >> (*)()>(&MyClass::run);
> >> }
> >>
> >> private:
> >>
> >> //static void run()
> >> void run()
> >> {
> >> ;
> >> }
> >>
> >> public:
> >> uint32_t someAddress;
> >> };
> >>
> >> This code compiles with a warning with GCC, but I get the following
> >> error with clang 3.2
> >>
> >> address_of_member_function.cpp:10:33: error: reinterpret_cast from
> >> 'void (MyClass::*)()' to 'void (*)()' is not allowed
> >> someAddress = (uint32_t)reinterpret_cast<void
> >> (*)()>(&MyClass::run);
> >>
> >> Is this a bug in clang or am I missing something?
> >>
> >> Regards,
> >> Salvatore
> >> _______________________________________________
> >> cfe-dev mailing list
> >> cfe-dev at cs.uiuc.edu
> >> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >
> >
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130519/b7214280/attachment.html>
More information about the cfe-dev
mailing list