1 // RUN: %clang_cc1 %s -I%S -std=c++2a -verify 2 3 namespace std { struct type_info; } 4 5 static_assert(requires { 0; }); 6 static_assert(requires { "aaaa"; }); 7 static_assert(requires { (0).da; }); // expected-error{{member reference base type 'int' is not a structure or union}} 8 9 struct A {}; 10 struct B { 11 B operator+(const B &other) const { return other; } 12 }; 13 struct C { 14 C operator+(C &other) const { return other; } 15 }; 16 17 template<typename T> requires requires (T a, const T& b) { a + b; } 18 // expected-note@-1{{because 'a + b' would be invalid: invalid operands to binary expression ('A' and 'const A')}} 19 // expected-note@-2{{because 'a + b' would be invalid: invalid operands to binary expression ('C' and 'const C')}} 20 struct r1 {}; 21 22 using r1i1 = r1<int>; 23 using r1i2 = r1<A>; // expected-error{{constraints not satisfied for class template 'r1' [with T = A]}} 24 using r1i3 = r1<B>; 25 using r1i4 = r1<C>; // expected-error{{constraints not satisfied for class template 'r1' [with T = C]}} 26 27 struct D { void foo() {} }; 28 29 template<typename T> requires requires (T a) { a.foo(); } 30 // expected-note@-1{{because 'a.foo()' would be invalid: no member named 'foo' in 'A'}} 31 // expected-note@-2{{because 'a.foo()' would be invalid: member reference base type 'int' is not a structure or union}} 32 // expected-note@-3{{because 'a.foo()' would be invalid: 'this' argument to member function 'foo' has type 'const D', but function is not marked const}} 33 struct r2 {}; 34 35 using r2i1 = r2<int>; // expected-error{{constraints not satisfied for class template 'r2' [with T = int]}} 36 using r2i2 = r2<A>; // expected-error{{constraints not satisfied for class template 'r2' [with T = A]}} 37 using r2i3 = r2<D>; 38 using r2i4 = r2<const D>; // expected-error{{constraints not satisfied for class template 'r2' [with T = const D]}} 39 40 template<typename T> requires requires { sizeof(T); } 41 // expected-note@-1{{because 'sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'void'}} 42 // expected-note@-2{{because 'sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'nonexistent'}} 43 struct r3 {}; 44 45 using r3i1 = r3<int>; 46 using r3i2 = r3<A>; 47 using r3i3 = r3<A &>; 48 using r3i4 = r3<void>; // expected-error{{constraints not satisfied for class template 'r3' [with T = void]}} 49 using r3i4 = r3<class nonexistent>; // expected-error{{constraints not satisfied for class template 'r3' [with T = nonexistent]}} 50 51 template<typename T> requires requires (T t) { 0; "a"; (void)'a'; } 52 struct r4 {}; 53 54 using r4i1 = r4<int>; 55 using r4i2 = r4<int[10]>; 56 using r4i3 = r4<int(int)>; 57 58 template<class T> void f(T) = delete; 59 template<class T> requires (sizeof(T) == 1) void f(T) { } 60 61 template<typename T> requires requires(T t) { f(t); } 62 // expected-note@-1{{because 'f(t)' would be invalid: call to deleted function 'f'}} 63 struct r5 {}; 64 65 using r5i1 = r5<int>; 66 // expected-error@-1 {{constraints not satisfied for class template 'r5' [with T = int]}} 67 using r5i2 = r5<char>; 68 69 template<typename T> 70 struct E { 71 struct non_default_constructible { non_default_constructible(T t) { } }; 72 }; 73 74 template<typename T> requires requires(T t) { typename E<T>::non_default_constructible{}; } 75 // expected-note@-1 {{because 'typename E<T>::non_default_constructible{}' would be invalid: no matching constructor for initialization of 'typename E<int>::non_default_constructible'}} 76 struct r6 {}; 77 78 using r6i1 = r6<int>; 79 // expected-error@-1 {{constraints not satisfied for class template 'r6' [with T = int]}} 80 81 template<typename T> requires requires(T t) { typename E<T>::non_default_constructible(); } 82 // expected-note@-1 {{because 'typename E<T>::non_default_constructible()' would be invalid: no matching constructor for initialization of 'typename E<int>::non_default_constructible'}} 83 struct r7 {}; 84 85 using r7i1 = r7<int>; 86 // expected-error@-1 {{constraints not satisfied for class template 'r7' [with T = int]}} 87 88 // C++ [expr.prim.req.simple] Example 89 namespace std_example { 90 template<typename T> concept C = 91 requires (T a, T b) { // expected-note{{because 'a' would be invalid: argument may not have 'void' type}} 92 a + b; // expected-note{{because 'a + b' would be invalid: invalid operands to binary expression ('int *' and 'int *')}} 93 }; 94 95 static_assert(C<int>); 96 template<C T> struct C_check {}; // expected-note{{because 'void' does not satisfy 'C'}} expected-note{{because 'int *' does not satisfy 'C'}} 97 using c1c1 = C_check<void>; // expected-error{{constraints not satisfied for class template 'C_check' [with T = void]}} 98 using c1c2 = C_check<int *>; // expected-error{{constraints not satisfied for class template 'C_check' [with T = int *]}} 99 } 100 101 // typeid() of an expression becomes potentially evaluated if the expression is 102 // of a polymorphic type. 103 class X { virtual ~X(); }; 104 constexpr bool b = requires (X &x) { static_cast<int(*)[(typeid(x), 0)]>(nullptr); }; 105 // expected-error@-1{{constraint variable 'x' cannot be used in an evaluated context}} 106 // expected-note@-2{{'x' declared here}} 107