Lets say I have the following code that initially deals with ints:
#define F_INT(x) _Generic((x), int: f_int(x), int *: f_intptr(x))
void f_int(int x) {printf("int\n");}
void f_intptr(int *x) {printf("intptr\n");}
int main()
{
int x;
F_INT(x);
}
The above code will display "int" because x
is declared as an int
. If x
is declared as pointer to int
, the above code will display "intptr". So far so good, even though the code produces warnings.
However if I switch everything to floats, then I get errors. If I declare x
as float
, I get:
error: incompatible type for argument 1 of ‘f_floatptr’ ... note: expected ‘float *’ but argument is of type ‘float’
If I declare x
as float *
, I get the opposite error.
_Generic
keyword seems to compile type check all selections even though the whole point should be that only one selection is chosen at compile time.
How do I get the above to work. I tried function pointers as a work around, but couldn't work around the issue.