(5.2.10/6) C++03 A pointer to a function can be explicitly converted to a pointer to a function of a different type. The effect of calling a function through a pointer to a function type (8.3.5) that is not the same as the type used in the definition of the function is undefined. Except that converting an rvalue of type "pointer to T1" to the type "pointer to T2" (where T1 and T2 are function types) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified. [Note: see also 4.10 for more details of pointer conversions.]
The following is what I'm trying to do, while it is clear that the result of converting fp1
to fp2
will produce a original pointer but at the same point the wording in standard goes "The result of such a pointer conversion is unspecified"
What does it mean by that?
int f() { return 42; }
int main()
{
void(*fp1)() = reinterpret_cast<void(*)()>(f);
int(*fp2)() = reinterpret_cast<int(*)()>(fp1);
// Safe to call the function ?
fp2();
}
You are misreading the standard, the "unspecified" part only applies to other conversions:
Except [special case] the result of such a pointer conversion is unspecified.
That [special case] is the one where you convert back to the original function pointer type, like in your example. In this special case the conversions yield the original pointer value, so it's ok to use it like in your example.
Only for other conversions the result is unspecified.