Member function pointers can not be reinterpret_cast to function pointers. (GCC requires the -pedantic-errors flag to enforce that, though.)
However GCC, Clang and MSVC seem to agree that casting a member function pointer to a reference to function pointer is ok. See the following example:
#include<type_traits>
struct A {
void f() {}
};
int main() {
auto x = &A::f;
auto y = reinterpret_cast<void(*&)()>(x);
// auto y = reinterpret_cast<void(*)()>(x);
static_assert(std::is_same_v<decltype(y), void(*)()>);
}
This program compiles on all three compilers, but fails to compile on all (with pedantic flags) when the out-commented line is used instead of the previous one.
I don't see any rule in the standard allowing this conversion. Is the program ill-formed and the compilers fail to diagnose it or is the program well-formed?
If the latter, what is the exact conversion sequence, where does the standard allow it and can the function pointer be converted back to the original type in order to call it or is the use of the reference for initialization of y already undefined behavior?
A::fis virtual?