Using the _Generic macro in C11 fails

I tried to overload the function using the C11 macro _Genericas follows:

int isPrim_int   (int num);  
int isPrim_lint  (long int num);  
int isPrim_llint (long long int num);

#define isPrim(_1, ...) _Generic((_1),                              \
                          int:              isPrim_int,             \
                          long int:         isPrim_lint,            \
                          long long int:    isPrim_llint)

But for some reason, use isPrim()always returns 1 when using certain three functions isPrim_int, isPrim_lintand isPrim_llintit works as intended. Any clues what is wrong with my use of the macro?

Thank you so much!

+4
source share
1 answer

"", , , , , "" , . _Generic , "" .

() _Generic . .

#define isPrim(_1, ...) _Generic((_1),                              \
                          int:              isPrim_int,             \
                          long int:         isPrim_lint,            \
                          long long int:    isPrim_llint)(_1)

,

int a = 42;
if (isPrim(a)(a))
   ...

, , , .

+3

Source: https://habr.com/ru/post/1689095/


All Articles