Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use _Generic in C++ code? [closed]

Tags:

c++

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.

like image 742
Curious Avatar asked Sep 02 '25 05:09

Curious


1 Answers

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.

like image 138
AnT Avatar answered Sep 04 '25 18:09

AnT