1

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);
11
  • What's the correct way to cast the function to a generic type to avoid this warning? -- Why do you believe there is a "correct" way? Maybe what you were doing was incorrect, and so far up until now you were never bitten by it. Applying C-style casts to avoid compiler warnings and errors is a sign that at some point, something may go wrong. Jul 12, 2019 at 22:41
  • Okay, then what's the correct way to insert function pointers that take a variety of different single class arguments into the same map? I need to find a cleaner way to make this work without rewriting everything, given that I don't completely understand how this whole system is supposed to work. This is the option processing part of a large commercial software code base. Jul 12, 2019 at 23:00
  • The way it should have been designed is to use function objects, and have the parameters part of the function object members. Then derive from a base object, and each child class overrides operator (). I know this is not an answer, but as a guide in how to prevent getting into this situation in the future. Jul 12, 2019 at 23:02
  • 1
    Here is a tiny example. One single map, multiple "functions", all having different signatures, and absolutely no casting was done, and no void*. The trick is that the parameters have been moved from the function invocation to the member variables, and the magic of operator() comes into play. Jul 12, 2019 at 23:20
  • I need to find a cleaner way to make this work without rewriting everything, given that I don't completely understand how this whole system is supposed to work. -- I don't think you need to understand the complete system. You know what that map and casting is supposed to do (theoretically). So it's a matter of reimplementing it knowing what the end results should be. It would be a different story if you had no idea what purpose the map had, what a function pointer is, etc. Jul 12, 2019 at 23:48

1 Answer 1

1

What's the correct way to cast the function to a generic type?

The answer is that there is no correct way. The warning is absolutely justified.

The point is that you have a typesafe function that you can pass only filename_t and you cast it to something that can take literally anything. The only way to not run into undefined bahavior is to still pass it a filename_t even though you now could pass anything else.

That yields the question of why you need to cast it to a generic type? You say that those functions get stored in a map with some type identifiers. Now, due to limited information it is hard to judge on this, but to me this sounds a little like artificially building a mechanism that choses the right function for you, while there is a perfectly working built in mechanism for this: function overload resolution.

So, at some point in the code you must have a filename_t object at hands. Then (I'm assuming) you infer some type identifier from it and use that to look up the proper function in a map. Then you pass your filename_t as a void* there, knowing that it will work.

I'm sure it's much more complex than this, but maybe you can identify this part of the logic and get rid of it: get rid of the map (at least that part that maps type identifiers to functions) and just call the function directly at a time where you still know the type (because then overload resolution will do the trick for you).

Templates can help you to keep the types instead of making everything void*.

4
  • Or if dynamic binding and virtual isn't an issue, function objects with an overridden call operator from a base class can be utilized. The parameters then need not be casted to void *, since the derived function object has this information. Either way, templates or dynamic binding, the OP's C-style casting stuff is a definite bad way to do this. Jul 12, 2019 at 23:32
  • The difficult part is that this has to work with member variables of a struct/class, including ints and floats. The code generation builds a map that can be used to serialize, etc. the set of member variables. It's built on top of the existing code, so I can't just modify every class member to inherit from some base class. Jul 13, 2019 at 0:19
  • Function overloading will work for some types, but the more complex types are defined externally and not available to the serialization code. I get compile errors that the function doesn't exist. Templates are similar, it's unclear where I can declare the template functions such that all of the code has access to them. Jul 13, 2019 at 0:20
  • 1
    For a real-world example of this, the Python C API's PyMethodDef table structure requires incompatible casts of this sort. docs.python.org/3/c-api/structures.html#c.PyMethodDef Aug 12, 2021 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.