I realized from this FAQ entry that one cannot convert a pointer to member function to/from void*
. The reason being pointers to members are not memory addresses exactly like pointers to data! Why so? Please help me get clarified. And this isn't necessarily with member functions but any normal C functions as well, isn't?
|
||||
show 5 more comments |
Pointers to member functions need to indicate whether the function is virtual, and allow virtual dispatch (perhaps by specifying the index into the vtable, rather than the address of a specific function) if so. This makes them more complicated than just an address.
Pointers to "normal" (non-member) functions may be converted to object pointers, but not portably. Quoting the standard:
On many platforms, a (non-member) function pointer is simply a memory address, and the conversion is well-defined. Some platforms have more exotic memory architectures - for example, separate memory spaces for instructions and data - and the conversion may not be allowed on those platforms. |
|||||||||||||||||||||
|
In the C++ standardAs the next FAQ says:
In §5.2.10/8 (of N3936 specifically) the standard specifies that this is indeed implementation defined:
Here the behavior is well specified. In the C standardThe C standard doesn't appear to contemplate the conversion from a function pointer to an object pointer. In fact it barely draws a line between them. It just states, at §6.3.2.3/8, that:
At this point the behavior almost seems to be unspecified. And then later in §6.5.9/6:
Here we can see the only trace of an actual difference in:
The whyAs for the "why", it appears to be dependent on the fact that some architectures simply have functions and objects in two address space. |
|||||
|
void *
is also a bad idea in both C and C++ – Ed Heal 12 hours ago