I was trying to learn how to use the "new" C11 Generic expressions but I ran into a wall.
Consider the following code:
#include <stdlib.h>
#include <stdio.h>
#define test(X, Y, c)
_Generic((X),
double: _Generic((Y),
double * : test_double,
default: test_double
),
int: _Generic((Y),
int * : test_int,
default: test_int
)
) (X, Y, c)
int test_double(double a, double *b, int c);
int test_int(int a, int *b, int c);
int test_double(double a, double *b, int c) { return 1; }
int test_int(int a, int *b, int c) { return 2; }
int main()
{
double *t = malloc(sizeof(double));
int *s = malloc(sizeof(int));
int a1 = test(3.4, t, 1);
int i = 3;
int a2 = test(i, s, 1);
printf("%d", a1);
printf("%d
", a2);
return 0;
}
This is all perfectly working, still I don't see why those default cases in "_Generic((Y), ..." are necessary while I can omit it at the end of "_Generic((X), ..." without consequences.
In fact, if I remove those two defaults I get an error (gcc 5.4.0) saying "selector of type ‘double *’ is not compatible with any association" while macro-expanding " int a1 = test(3.4, t, 1);" and the same thing with "int *" while macro-expanding test(i, s, 1)
Is "default" really necessary or am I missing something?
In the first case, why the hell should it be? If I have only test_double and test_int that can be called why should I put a default case for something that should never even compile?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…