[cfe-dev] Fwd: Can't get address of member-function

salvatore benedetto salvatore.benedetto at gmail.com
Sun May 19 02:31:19 PDT 2013


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



More information about the cfe-dev mailing list