aghast
aghast

Reputation: 15310

How can I pass either type or value to _Generic()

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.

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:

Is there some twisted C syntax horror that will permit the use of either type names or expressions in a _Generic-expression?

Upvotes: 5

Views: 115

Answers (1)

David Grayson
David Grayson

Reputation: 87416

Your compound literal idea will work if you combine it with typeof. However, please note that typeof is a a GCC C language extension, so it might not work in every C compiler.

#include <stdio.h>

typedef struct Foo {
  int mx;
} Foo;

#define F(x) _Generic((typeof(x)){0}, \
  int: 1, \
  long: 2, \
  struct Foo: 3)

int main()
{
  printf("%d\n", F(0));         // 1
  printf("%d\n", F(int));       // 1
  printf("%d\n", F((long)0));   // 2
  printf("%d\n", F(long));      // 2
  printf("%d\n", F((Foo){1}));  // 3
  printf("%d\n", F(Foo));       // 3
}

Upvotes: 3

Related Questions