typecasting function pointer to void*

Discussion in 'C++' started by WittyGuy, Jul 27, 2006.

  1. WittyGuy

    WittyGuy Guest

    How to typecast a "function pointer" to "const void*" type in C++ way?

    int MyFunction (double money); // Function prototype

    const void* arg = (const void*)MyFunction; // type casting function
    pointer to const void* in C-style

    void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
    pointer in C-style
    (*pFunc)(); // Calling the function after type casting is done

    Is typecasting like above is safe anyway?

    regards,
    Sukumar
     
    WittyGuy, Jul 27, 2006
    #1
    1. Advertising

  2. WittyGuy schrieb:

    > How to typecast a "function pointer" to "const void*" type in C++ way?
    >
    > int MyFunction (double money); // Function prototype


    I suggest you to create a typedef for the function pointer:
    typedef int (*TMyFunction) ( double );

    And then use it!
    void * ptr = static_cast<void*>(&some_func);
    ....
    (static_cast<TMyFunction>(ptr))(23.42):
     
    Florian Stinglmayr, Jul 27, 2006
    #2
    1. Advertising

  3. WittyGuy

    Kai-Uwe Bux Guest

    WittyGuy wrote:

    > How to typecast a "function pointer" to "const void*" type in C++ way?


    There is no "how".

    > int MyFunction (double money); // Function prototype
    >
    > const void* arg = (const void*)MyFunction; // type casting function
    > pointer to const void* in C-style
    >
    > void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
    > pointer in C-style
    > (*pFunc)(); // Calling the function after type casting is done


    You are casting back to a different signature. Even if there was a roundtrip
    guarantee through void* (which there is not), this conversion would be
    unspecified.

    > Is typecasting like above is safe anyway?


    No: In C++ there is no round-trip guarantee for pointer-to-function or
    pointer-to-member-function to void* and back. Just don't do it.

    Function pointers are convertible to a different signature, however, the
    result of such conversion cannot be used: it can only be converted back.
    However, this does allow you to use void(*)(void) as a universal function
    pointer to and from which you can cast (reinterpret_cast is the one you
    might want to use) [5.2.10/6].


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jul 27, 2006
    #3
  4. WittyGuy

    Rolf Magnus Guest

    WittyGuy wrote:

    > How to typecast a "function pointer" to "const void*" type in C++ way?


    Not possible in standard C++. You can only cast object pointers to void*,
    not function pointers.

    > int MyFunction (double money); // Function prototype
    >
    > const void* arg = (const void*)MyFunction; // type casting function
    > pointer to const void* in C-style
    >
    > void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
    > pointer in C-style
    > (*pFunc)(); // Calling the function after type casting is done
    >
    > Is typecasting like above is safe anyway?


    It is not safe at all. Assuming that your compiler supports the conversion
    function pointer -> void pointer as an extension (which is not too
    uncommon), it would still be very dangerous, because you're calling a
    function that takes one argument through a pointer to function that takes
    no arguments.
     
    Rolf Magnus, Jul 27, 2006
    #4
  5. WittyGuy

    Guest

    I think you can use reinterpret cast.
    Rolf Magnus wrote:
    > WittyGuy wrote:
    >
    > > How to typecast a "function pointer" to "const void*" type in C++ way?

    >
    > Not possible in standard C++. You can only cast object pointers to void*,
    > not function pointers.
    >
    > > int MyFunction (double money); // Function prototype
    > >
    > > const void* arg = (const void*)MyFunction; // type casting function
    > > pointer to const void* in C-style
    > >
    > > void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
    > > pointer in C-style
    > > (*pFunc)(); // Calling the function after type casting is done
    > >
    > > Is typecasting like above is safe anyway?

    >
    > It is not safe at all. Assuming that your compiler supports the conversion
    > function pointer -> void pointer as an extension (which is not too
    > uncommon), it would still be very dangerous, because you're calling a
    > function that takes one argument through a pointer to function that takes
    > no arguments.
     
    , Jul 27, 2006
    #5
  6. WittyGuy

    Default User Guest

    wrote:

    > I think you can use reinterpret cast.


    Don't top-post.


    You can get the compiler to shut up about the illegal conversion, but
    that doesn't mean it will work.




    Brian
     
    Default User, Jul 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    600
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    836
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    880
    S.Tobias
    Jul 22, 2005
  4. Abhishek
    Replies:
    16
    Views:
    893
    Jordan Abel
    Jan 26, 2006
  5. Replies:
    1
    Views:
    434
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page