23 typedef int (*fptr
)(int);
24 typedef void (*vfptr
)(int);
29 void void_foo(int i
) {}
31 typedef int int_type1
;
33 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
38 signed long int l
= 2;
40 const int * const ptr
;
44 i
= _Generic(a
, int: a_f
, const int: b_f
)();
46 i
= _Generic(a
, int: a_f() / 2, const int: b_f() / 2);
48 i
= _Generic(ptr
, int *:1, int * const:2, default:20);
52 i
= _Generic(titi
, struct a
:1, struct b
:2, default:20);
54 i
= _Generic(i2
, char: 1, int : 0);
56 i
= _Generic(a
, char:1, int[4]:2, default:5);
58 i
= _Generic(17, int :1, int **:2);
60 i
= _Generic(17L, int :1, long :2, long long : 3);
62 i
= _Generic("17, io", char *: 3, const char *: 1);
64 i
= _Generic(ti
, const unsigned char *:1, const char *:4, char *:3,
65 const signed char *:2);
67 printf("%s\n", _Generic(i
+ 2L, long: "long", int: "int",
68 long long: "long long"));
69 i
= _Generic(l
, long: 1, int: 2);
71 i
= _Generic(foo
, fptr
: 3, int: 4, vfptr
: 5);
73 i
= _Generic(void_foo
, fptr
: 3, int: 4, vfptr
: 5);
76 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
78 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
79 struct { _Bool x_0
: _Generic(({0;}),default:1); } my_x
;
81 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
82 int const x
= 42; __typeof((__typeof(x
))x
) *xp
= 0; (void)_Generic(xp
, int*: 0); //casts lose top-level qualifiers
86 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
87 //combining of qualifiers
88 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
89 //nul-ptr constant selects other type
90 _Generic( 0?(long*)0:0, long*: (void)0);
91 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
93 //void ptrs get chosen preferentially; qualifs still combine
94 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
95 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
96 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
98 //ptrs to incomplete types get completed
99 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
100 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
103 /* completion shouldn't affect the type of decl */
105 _Generic(argv
, char**: (void)0);
106 _Generic(0?(char const*)0:argv
[0], char const*: (void)0);
107 _Generic(argv
, char**: (void)0);
111 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
112 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
113 (void)(sizeof(struct { int x
:_Generic( 0?ar
: (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
114 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[4])0 : ar
, int (*)[4]:+1, int (*)[5]:(void)0); }));
115 (void)(sizeof(struct { int x
:_Generic( 0?(int (*)[5])0 : ar
, int (*)[5]:+1, int (*)[4]:(void)0); }));