23 typedef int (*fptr
)(int);
24 typedef void (*vfptr
)(int);
29 void void_foo(int i
) {}
31 typedef int int_type1
;
36 return _Generic(t
, __typeof__( ((void)0, (T
){0}) ) : 1 );
39 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
44 signed long int l
= 2;
46 const int * const ptr
;
51 i
= _Generic(a
, int: a_f
, const int: b_f
)();
53 i
= _Generic(a
, int: a_f() / 2, const int: b_f() / 2);
55 i
= _Generic(ptr
, int *:1, int * const:2, default:20);
59 i
= _Generic(titi
, struct a
:1, struct b
:2, default:20);
61 i
= _Generic(i2
, char: 1, int : 0);
63 i
= _Generic(a
, char:1, int[4]:2, default:5);
65 i
= _Generic(17, int :1, int **:2);
67 i
= _Generic(17L, int :1, long :2, long long : 3);
69 i
= _Generic("17, io", char *: 3, const char *: 1);
71 i
= _Generic(ti
, const unsigned char *:1, const char *:4, char *:3,
72 const signed char *:2);
74 printf("%s\n", _Generic(i
+ 2L, long: "long", int: "int",
75 long long: "long long"));
76 i
= _Generic(l
, long: 1, int: 2);
78 i
= _Generic(foo
, fptr
: 3, int: 4, vfptr
: 5);
80 i
= _Generic(void_foo
, fptr
: 3, int: 4, vfptr
: 5);
83 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
85 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
86 struct { _Bool x_0
: _Generic(({0;}),default:1); } my_x
;
88 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
89 int const x
= 42; __typeof((__typeof(x
))x
) *xp
= 0; (void)_Generic(xp
, int*: 0); //casts lose top-level qualifiers
93 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
94 //combining of qualifiers
95 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
96 //nul-ptr constant selects other type
97 _Generic( 0?(long*)0:0, long*: (void)0);
98 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
100 //void ptrs get chosen preferentially; qualifs still combine
101 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
102 //but this is no null-ptr constant, so fallback to void-choice
104 _Generic( 1?(void*)(i
*0LL):&i
, void*:0);
105 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
106 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
108 //ptrs to incomplete types get completed
109 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
110 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
113 /* completion shouldn't affect the type of decl */
115 _Generic(argv
, char**: (void)0);
116 _Generic(0?(char const*)0:argv
[0], char const*: (void)0);
117 _Generic(argv
, char**: (void)0);
121 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
122 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
123 (void)(sizeof(struct { int x
:_Generic( 0?ar
: (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
124 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : ar
, int (*)[4]:+1, int (*)[5]:(void)0); }));
125 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[5])0 : ar
, int (*)[5]:+1, int (*)[4]:(void)0); }));
128 printf ("%d\n", f(t
));