3
void ( * pFunc_pcInt ) ( int const * ) = nullptr ; 
void ( * pFunc_pInt ) ( int * ) = reinterpret_cast< void ( * ) ( int * ) >( pFunc_pcInt ) ;

Does such conversion lead to undefined behavior.?

1 Answer 1

3

As from the standard (working draft, emphasis mine):

A function pointer can be explicitly converted to a function pointer of a different type. [ Note: The effect of calling a function through a pointer to a function type ([dcl.fct]) that is not the same as the type used in the definition of the function is undefined.  — end note ] [...]

And, of course, void(int const *) and void(int *) are different types.

Something similar comes from one of the most known online reference (emphasis mine):

Any pointer to function can be converted to a pointer to a different function type. Calling the function through a pointer to a different function type is undefined, but converting such pointer back to pointer to the original function type yields the pointer to the original function.

In your specific case it doesn't matter that much, for you are assigning nullptr to the function pointer. Invoking it would lead to an error in any case.
That being said, if you had assigned a valid function pointer to pFunc_pcInt, invoking it through the converted pointer pFunc_pInt would have been an UB.

Sign up to request clarification or add additional context in comments.

5 Comments

i thought about something like exception, which is hidden somewhere in extremely unrelated paragraph, but, seems, that is not such case.
@GreenTree: Many C compilers, especially those intended for embedded or systems programming, expressly document calling conventions in a way that would implicitly define behaviors in cases where calling code is specified as setting things up in the way that the called function requires. Because of name mangling and other issues, calling conventions are more likely to be unspecified in C++ except when functions and pointers are declared as extern "C" {...}.
@supercat, what did you exactly mean "calling conventions are more likely to be unspecified in C++".? and how does calling conventions relate to name mangling.?
@GreenTree: On most platforms, code generated by one C compiler is expected to be able to call code generated by another, but the same is not true of C++ compilers except for functions qualified as extern "C" (which are expected to be work with other C or C++ compilers). Compatibility between C compilers is obtained by specifying how calling code must make function arguments available, and how called functions will retrieve them. If a platform defines no way in which the code to call functions void func1(int *p) and void func2(int const*p) would differ...
...then the effects of a call using either prototype would be identical when calling to an external function. The Standard would not forbid compilers that can see both the calling and called function from behaving in arbitrary function when their declarations don't match, even if the calling code would otherwise be identical for both, but many compilers which would have no reason to care, don't care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.