I have a code generation script that was written by someone else around 2008 and has worked fine mostly unchanged since then. Just recently I tried compiling with gcc9 and I see 7300 warnings for "cast between incompatible function types" in the generated code.
The code takes a set of function pointers and various type identifiers and inserts everything into a big map that's used later for option serialization, printing, etc. Many functions and variables are cast and stored as some form of void *. The code otherwise compiles with no errors and works properly.
I tried various C-style casts, reinterpret_cast, and casting the function pointer to void(*)(void), but none of them remove the warnings. What's the correct way to cast the function to a generic type to avoid this warning? The only solution I can come up with is disabling -Wcast-function-type.
Here is one example line that generates warnings:
pim.must_find("input_fn")->set_introspect_info( sizeof(filename_t), ((char *)(&p_cn->input_fn)) - ((char *)p_cn), 0,invalid_offset, (str_from_base_t *)str_from_filename_t, (send_base_t *)send_filename_t, (recv_base_t *)recv_filename_t, (val_from_param_t *)val_from_param_filename_t, 0);
The warning is related to these two functions:
typedef std::string str_from_base_t( void const * );
std::string str_from_filename_t( filename_t const & v ) { return v; }
Where filename_t is a class that interits from std::string.
Note that there are dozens of different str_from_***() functions that have different classes as arguments.
The warning I get is:
../src/gen/DEFReader_PostParam.cc: In function 'void croix::DEFReaderCLI_introspect_pim_init()':
../src/gen/DEFReader_PostParam.cc:25:153: warning: cast between incompatible function types from 'std::string (*)(const croix::filename_t&)' {aka 'std::basic_string<char> (*)(const croix::filename_t&)'} to 'std::string (*)(const void*)' {aka 'std::basic_string<char> (*)(const void*)'} [-Wcast-function-type]
25 | pim.must_find("input_fn")->set_introspect_info( sizeof(filename_t), ((char *)(&p_cn->input_fn)) - ((char *)p_cn), 0,invalid_offset, (str_from_base_t *)str_from_filename_t, (send_base_t *)send_filename_t, (recv_base_t *)recv_filename_t, (val_from_param_t *)val_from_param_filename_t, 0);
operator (). I know this is not an answer, but as a guide in how to prevent getting into this situation in the future.void*. The trick is that the parameters have been moved from the function invocation to the member variables, and the magic ofoperator()comes into play.