xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/generic-selection.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc template <typename T, typename U = void*>
4*f4a2713aSLionel Sambuc struct A {
5*f4a2713aSLionel Sambuc   enum {
6*f4a2713aSLionel Sambuc     id = _Generic(T(), // expected-error {{controlling expression type 'char' not compatible with any generic association type}}
7*f4a2713aSLionel Sambuc         int: 1, // expected-note {{compatible type 'int' specified here}}
8*f4a2713aSLionel Sambuc         float: 2,
9*f4a2713aSLionel Sambuc         U: 3) // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}
10*f4a2713aSLionel Sambuc   };
11*f4a2713aSLionel Sambuc };
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc static_assert(A<int>::id == 1, "fail");
14*f4a2713aSLionel Sambuc static_assert(A<float>::id == 2, "fail");
15*f4a2713aSLionel Sambuc static_assert(A<double, double>::id == 3, "fail");
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc A<char> a1; // expected-note {{in instantiation of template class 'A<char, void *>' requested here}}
18*f4a2713aSLionel Sambuc A<short, int> a2; // expected-note {{in instantiation of template class 'A<short, int>' requested here}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc template <typename T, typename U>
21*f4a2713aSLionel Sambuc struct B {
22*f4a2713aSLionel Sambuc   enum {
23*f4a2713aSLionel Sambuc     id = _Generic(T(),
24*f4a2713aSLionel Sambuc         int: 1, // expected-note {{compatible type 'int' specified here}}
25*f4a2713aSLionel Sambuc         int: 2, // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}
26*f4a2713aSLionel Sambuc         U: 3)
27*f4a2713aSLionel Sambuc   };
28*f4a2713aSLionel Sambuc };
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc template <unsigned Arg, unsigned... Args> struct Or {
31*f4a2713aSLionel Sambuc   enum { result = Arg | Or<Args...>::result };
32*f4a2713aSLionel Sambuc };
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc template <unsigned Arg> struct Or<Arg> {
35*f4a2713aSLionel Sambuc   enum { result = Arg };
36*f4a2713aSLionel Sambuc };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc template <class... Args> struct TypeMask {
39*f4a2713aSLionel Sambuc   enum {
40*f4a2713aSLionel Sambuc    result = Or<_Generic(Args(), int: 1, long: 2, short: 4, float: 8)...>::result
41*f4a2713aSLionel Sambuc   };
42*f4a2713aSLionel Sambuc };
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc static_assert(TypeMask<int, long, short>::result == 7, "fail");
45*f4a2713aSLionel Sambuc static_assert(TypeMask<float, short>::result == 12, "fail");
46*f4a2713aSLionel Sambuc static_assert(TypeMask<int, float, float>::result == 9, "fail");
47