开发者

Stopping Compiler from generating errors in a C++ code that has legacy C code

开发者 https://www.devze.com 2023-04-04 02:41 出处:网络
I am porting a legacy C program to C++. However, the compiler is not happy and requires additional typecastings for C++. For example, I have this function...

I am porting a legacy C program to C++. However, the compiler is not happy and requires additional typecastings for C++. For example, I have this function...

 void foreach_element_in_patch(Patch *patch, void (*func)(), 
long arg1, long process_id);

In the original C Code, it is used like this...

 foreach_element_in_patch( patch, display_interactions_in_element, 
mode, process_id );

However, for C++ I need to typecast the second argument to stop the compiler from gernerating an error.

 foreach_element_in_patch( patch, (void (*)())display_interactions_in_element, 
mode, process_id );

The error generated by the compiler is the following...

invalid conversion from ‘void (*)(Patch*, long int, long int)’ to ‘void开发者_Python百科 (*)()’

Now is there a way to ask the compiler not to generate errors for such things. I have tried prefixing this function with extern "C" but still the C++ compiler is not happy. My applications is loaded with such code and I do not have the time to adjust so much code.


The error is quite clear, and your cast is invalid. You can't use a function that takes arguments as a function that doesn't take arguments.

For your code to work, you need to either:

  1. Create a new function that calls display_interactions_in_element with sensible defaults.
  2. Or use a capture-less and argument-less lambda that does the same (it might not be supported by your compiler, yet, though).

Note that if your code tries to call that function as if it took arguments later, it means the code is fundamentally broken.


Yes, there is a way. Compile your code as C.

C++ has stricter type safety rules, and requires you to explicitly cast types that could be implicitly converted in C.

So either compile the code as C, or make the modifications necessary for the code to be valid C++, and compile it as C++.

You should be able to compile your C files as C, your C++ files as C++, and then link them together without a problem.

But when you try to compile your C files as C++, the compiler is going to tell you if your code isn't valid C++.


I think your error comes from the fact that the function signature is has a different meaning in C and C++. The second parameter specification

void (*func)()

in C means "a function with an unknown number and type of parameters" and in C++ it means "a function without arguments". So these are quite different and it must crash.

The error is not in the lack of type safety of C, but in your code. Don't do that. Use strict prototypes, even in C. The correct parameter specification for both languages is just

void (*func)(Patch*, long int, long int)

that's it and everything is just blindly hacking around.


The easiest thing to do is to change the function prototypes and headers to declare the correct type of function pointer. So for example, for

void foreach_element_in_patch(Patch *patch, void (*func)(), 
 long arg1, long process_id);

I just need to change it to

void foreach_element_in_patch(Patch *patch, void (*)(Patch*, long int, long int), 
 long arg1, long process_id);

Instead of making changes at every function call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号