The sizeof builtin can take either a type or an expression, and will return the appropriate value.
I'd like to build a macro that uses _Generic() expressions to do something similar. In particular, I'd like to be able to pass either a type name or a value as the parameter, just like sizeof.
As a trivial example, I can do something like this:
#define F(x) _Generic((x)+0, int: 1, long: 2)
That works because (x)+0 can be either an arithmetic expression or a type cast.
- (1)+0 -> 1+0 -> 1 -> int: 1
- (long)+0 -> 0L -> long: 2
But that doesn't work when the type is struct Foo.
I could use composite literal syntax, with curly braces: (x){0} but that fails for literal values:
- (long){0} - works
- (struct Foo){0} - works
- (1){0} - not so much. :-(
Is there some twisted C syntax horror that will permit the use of either type names or expressions in a _Generic-expression?