Is it possible to map function pointer to std::string
in standard conformant C++?
Casting function pointers to object pointers and even doing pointer arithmetic is off limits with function pointers but in this related question
How to format a function pointer?
the following code is given:
int (*funcptr)() = main;
unsigned char *p = (unsigned char *)&funcptr;
int i;
for (i = 0; i < sizeof funcptr; i++)
{
printf("%02x ", p[i]);
}
putchar('\n');
That code has withstood peer scrutiny. I'm a bit confused by it. Accessing a function pointer with array syntax is legal but pointer arithmetic is not?
Here's what I have at the moment. My compiler doesn't complain and with a sample size of 1 things seem well behaved.
template<class F>
std::string fptr_to_str(F fptr)
{
auto fptr_bytes = (const char*)fptr;
return {fptr_bytes, sizeof(fptr_bytes)};
}