I recently wrote code at night and made a mistake. I needed to write in the virtual memory of the process a pointer to the desired function. I did it like this:
uint64_t *some_mapped_memory = ...; some_mapped_memory[0] = reinterpret_cast<uint64_t>(myFunc); The error is that I did not put & in front of the function name. And not the pointer, but the function itself. However, this code worked just as expected.
Moreover, I later made a test:
#include <cstdio> void f() {} int main() { void (*fnc1)() = f; void (*fnc2)() = &f; printf("%p %p\n", f, &f); return 0; } Not only did printf print two identical values, the code above also compiled without problems.
I googled a little, but found nothing about it. Honestly, there is no gcc or other compiler at hand, tested with visual c++ . What does the standard think about my code? Well, is it obliged to work on another compiler, or is only &f valid? And why does my development environment show that fnc1 and fnc2 are both void(*)() , but auto fnc3 = f is void() ?
In general, what I have shown is some kind of evil clever transformation described by the standard, or an evil clever transformation that can visual c++ ?