23 typedef int (*fptr
)(int);
29 typedef int int_type1
;
31 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
36 signed long int l
= 2;
38 const int * const ptr
;
42 i
= _Generic(a
, int: a_f
, const int: b_f
)();
44 i
= _Generic(a
, int: a_f() / 2, const int: b_f() / 2);
46 i
= _Generic(ptr
, int *:1, int * const:2, default:20);
50 i
= _Generic(titi
, struct a
:1, struct b
:2, default:20);
52 i
= _Generic(i2
, char: 1, int : 0);
54 i
= _Generic(a
, char:1, int[4]:2, default:5);
56 i
= _Generic(17, int :1, int **:2);
58 i
= _Generic(17L, int :1, long :2, long long : 3);
60 i
= _Generic("17, io", char *: 3, const char *: 1);
62 i
= _Generic(ti
, const unsigned char *:1, const char *:4, char *:3,
63 const signed char *:2);
65 printf("%s\n", _Generic(i
+ 2L, long: "long", int: "int",
66 long long: "long long"));
67 i
= _Generic(l
, long: 1, int: 2);
69 i
= _Generic(foo
, fptr
: 3, int: 4);
72 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
74 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
75 struct { _Bool x_0
: _Generic(({0;}),default:1); } my_x
;
77 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
78 int const x
= 42; __typeof((__typeof(x
))x
) *xp
= 0; (void)_Generic(xp
, int*: 0); //casts lose top-level qualifiers
82 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
83 //combining of qualifiers
84 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
85 //nul-ptr constant selects other type
86 _Generic( 0?(long*)0:0, long*: (void)0);
87 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
89 //void ptrs get chosen preferentially; qualifs still combine
90 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
91 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
92 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
94 //ptrs to incomplete types get completed
95 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
96 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
99 /* completion shouldn't affect the type of decl */
101 _Generic(argv
, char**: (void)0);
102 _Generic(0?(char const*)0:argv
[0], char const*: (void)0);
103 _Generic(argv
, char**: (void)0);
107 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
108 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
109 (void)(sizeof(struct { int x
:_Generic( 0?ar
: (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
110 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : ar
, int (*)[4]:+1, int (*)[5]:(void)0); }));
111 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[5])0 : ar
, int (*)[5]:+1, int (*)[4]:(void)0); }));