I find that to instantiate a _Generic, the source file needs to be a .c file and compiled with gcc -std=c11. Naming it as a .cpp file and using g++ -std=c++11 does not work. Neither does g++ -std=c11 (which is expected because the -std=c11 switch is only applicable for .c files). What is a good way to define _Generic functions in a C++ library, and let a C++ application use the library? The intent is to support C applications but without abandoning support for C++ applications.
There's no _Generic
in C++ and you cannot use it in C++ code. If you want to design cross-compilable code (e.g. header files), either avoid _Generic
entirely, or use #ifdef __cplusplus
to provide two independent (or semi-independent) versions of the same code for C and C++ separately, e.g. use function overloading on C++ side instead of _Generic
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With