logoalt Hacker News

akdev1l06/16/20252 repliesview on HN

It’s not a warning, it’s a compile time error and I am not even using -Wall -Werror

I also believe there are platforms where a function pointer and a data pointer are not the same but idk about such esoteric platforms first hand (seems Itanium had that: https://stackoverflow.com/questions/36645660/why-cant-i-cast...)

Though my point was only that this code will not compile as is with whatever clang Apple ships*

I am not really sure how to get it to compile tbqh

Some further research ( https://www.kdab.com/how-to-cast-a-function-pointer-to-a-voi...) suggest it should be done like so:

> auto fptr = &f; void a = reinterpret_cast<void &>(fptr);

edit: I tried with GCC 15 and that compiled successfully


Replies

gpderetta06/16/2025

FWIW, POSIX practically requires void otr and function otr inter-convertibility hence the support from GCC.

show 1 reply
comex06/16/2025

It should just be

    res.fn = (void *)fn;
`res.fn` is of type `void *`, so that's what the code should be casting to. Casting to `func0Ptr` there seems to just be a mistake. Some compilers may allow the resulting function pointer to then implicitly convert to `void *`, but it's not valid in standard C++, hence the error.

Separately from that, if you enable -Wpedantic, you can get a warning for conversions between function and data pointers even if they do use an explicit cast, but that's not the default.