In the following code:
struct Person {
char* name;
int age;
};
struct Book {
char* title;
char* author;
};
#define MYTYPE(X) _Generic((X), int: "int", float: "float", double: "double", struct Book: "book", struct Person: "person", default: "other")
The following works:
struct Book ulysses = {"ulysses", "james"};
printf("%s\n", MYTYPE(ulysses));
struct Person jim;
jim = (struct Person) {"Tom", 20};
printf("%s\n", MYTYPE(jim));
However, if I try passing a compound literal it fails:
printf("%s\n", MYTYPE((struct Person){"Tom", 10}));
gen.c:25:53: error: macro "MYTYPE" passed 2 arguments, but takes just 1
printf("%s\n", MYTYPE((struct Person){"Tom", 10}));
............................................................................... ^
What seems to be the issue with the passing of the struct Person
to the MYTYPE
macro?
Update: it seems double wrapping the expression in parens fixes this but I'm not sure why that's required:
printf("%s\n", MYTYPE(((struct Person){"Tom", 10})));