[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / generic-selection.cpp
blob7b4fad65a1811efead002b285dba0fadbb4acc62
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 template <typename T, typename U = void*>
4 struct A {
5 enum {
6 id = _Generic(T(), // expected-error {{controlling expression type 'char' not compatible with any generic association type}}
7 int: 1, // expected-note {{compatible type 'int' specified here}}
8 float: 2,
9 U: 3) // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}
13 static_assert(A<int>::id == 1, "fail");
14 static_assert(A<float>::id == 2, "fail");
15 static_assert(A<double, double>::id == 3, "fail");
17 A<char> a1; // expected-note {{in instantiation of template class 'A<char>' requested here}}
18 A<short, int> a2; // expected-note {{in instantiation of template class 'A<short, int>' requested here}}
20 template <typename T, typename U>
21 struct B {
22 enum {
23 id = _Generic(T(),
24 int: 1, // expected-note {{compatible type 'int' specified here}}
25 int: 2, // expected-error {{type 'int' in generic association compatible with previously specified type 'int'}}
26 U: 3)
30 template <unsigned Arg, unsigned... Args> struct Or {
31 enum { result = Arg | Or<Args...>::result };
34 template <unsigned Arg> struct Or<Arg> {
35 enum { result = Arg };
38 template <class... Args> struct TypeMask {
39 enum {
40 result = Or<_Generic(Args(), int: 1, long: 2, short: 4, float: 8)...>::result
44 static_assert(TypeMask<int, long, short>::result == 7, "fail");
45 static_assert(TypeMask<float, short>::result == 12, "fail");
46 static_assert(TypeMask<int, float, float>::result == 9, "fail");
49 struct Test {
50 int i;
53 void unreachable_associations(const int i, const Test t) {
54 // FIXME: it's not clear to me whether we intended to deviate from the C
55 // semantics in terms of how qualifiers are handled, so this documents the
56 // existing behavior but perhaps not the desired behavior.
57 static_assert(
58 _Generic(i,
59 const int : 1, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}
60 volatile int : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'volatile int' will never be selected because it is qualified}}
61 int[12] : 3, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'int[12]' will never be selected because it is of array type}}
62 int : 4,
63 default : 5
64 ) == 4, "we had better pick int, not const int!");
65 static_assert(
66 _Generic(t,
67 Test : 1,
68 const Test : 2, // Ok in C++, warned in C
69 default : 3
70 ) == 2, "we had better pick const Test, not Test!"); // C++-specific result
73 namespace GH55562 {
74 struct S { // expected-note {{declared here}}
75 int i;
78 void func(struct S s) {
79 // We would previously reject this because the parser thought 'struct S :'
80 // was the start of a definition (with a base class specifier); it's not, it
81 // is an elaborated type specifier followed by the association's value and
82 // it should work the same as in C.
83 (void)_Generic(s, struct S : 1);
85 // The rest of these cases test that we still produce a reasonable diagnostic
86 // when referencing an unknown type or trying to define a type in other ways.
87 (void)_Generic(s, struct T : 1); // expected-error {{type 'struct T' in generic association incomplete}}
88 (void)_Generic(s, struct U { int a; } : 1); // expected-error {{'U' cannot be defined in a type specifier}}
89 (void)_Generic(s, struct V : S); // expected-error {{'S' does not refer to a value}}
90 (void)_Generic(s, struct W : S { int b; } : 1); // expected-error {{expected '(' for function-style cast or type construction}}
92 } // namespace GH55562