C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

typecasting function pointer to void*

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
WittyGuy
Guest





PostPosted: Thu Jul 27, 2006 9:10 am    Post subject: typecasting function pointer to void* Reply with quote



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
Back to top
Florian Stinglmayr
Guest





PostPosted: Thu Jul 27, 2006 9:10 am    Post subject: Re: typecasting function pointer to void* Reply with quote



WittyGuy schrieb:

Quote:
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):
Back to top
Kai-Uwe Bux
Guest





PostPosted: Thu Jul 27, 2006 9:11 am    Post subject: Re: typecasting function pointer to void* Reply with quote



WittyGuy wrote:

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

There is no "how".

Quote:
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.

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.