3 ms·
I think none of these points are demonstrated in the post hence I fail to visualize it Also I copy pasted the code from the post and I got this: test.cpp:70:1
by akdev1l 1y ago
I think none of these points are demonstrated in the post hence I fail to visualize it
Also I copy pasted the code from the post and I got this:
test.cpp:70:14: error: assigning to 'void ' from 'func0Ptr' (aka 'void ()(void *)') converts between void pointer and function pointer
70 | res.fn = (func0Ptr)fn;
- cryptonector 1y ago> test.cpp:70:14: error: assigning to 'void ' from 'func0Ptr' (aka 'void ()(void *)') converts between void pointer and function pointer 70 | res.fn = (func0Ptr)fn; This warning is stupid. It's part of the "we reserve the right to change the size of function pointers some day so that we can haz closures, so you can't assume that function pointers and data pointers are the same size m'kay?" silliness. And it is silly: because the C and C++ committees will never be able to change the size of function pointers, not backwards-compatibly. It's not that I don't wish they could. It's that they can't.
- akdev1l 1y agoIt’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-a-function-pointer-to-void 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-void/ 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
- comex 1y agoIt 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.
- gpderetta 1y agoFWIW, POSIX practically requires void otr and function otr inter-convertibility hence the support from GCC.
- wahern 1y agoNot practically, but literally: Note that conversion from a void * pointer to a function pointer as in: fptr = (int (*)(int))dlsym(handle, "my_function"); is not defined by the ISO C standard. This standard requires this conversion to work correctly on conforming implementations.
- gpderetta 1y agoI remembered the dlsym requirement, I wasn't sure it was de-jure.
- kjksf 1y agoThanks, fixed. It works in msvc but as someone pointed out, it was a typo and was meant to be (void*) cast.