在 2024-09-18 21:02, Martin Storsjö 写道:
Hmm, can you give a godbolt example or similar, for where the compilers diverge 
about warnings here?

With Clang, I don't seem to be able to get any warnings for either casts via (void(*)(void)) or (void*), with either C or C++, with -Wall -Wextra -pedantic.


Given

   ```
   typedef int my_function(int, int);

   void*
   try_casting(my_function* fp)
   {
     return (void*) fp;
   }
   ```

GCC shows a warning if this is compiled as C:
(https://gcc.godbolt.org/z/5916TPrGc)

   ```
   <source>: In function 'try_casting':
<source>:6:10: warning: ISO C forbids conversion of function pointer to object pointer type [-Wpedantic]
       6 |   return (void*) fp;
         |          ^
   ```

And Clang shows a warning if this is compiled as C++:
(https://gcc.godbolt.org/z/WxrebaPse)

   ```
<source>:6:10: warning: cast between pointer-to-function and pointer-to-object is an extension [-Wpedantic]
     return (void*) fp;
            ^~~~~~~~~~
   1 warning generated.
   ```

Technically, this is undefined behavior in C and conditionally-supported 
feature in C++.


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to