xref: /llvm-project/clang/test/CXX/expr/expr.prim/expr.prim.id/p4.cpp (revision 6da3d66f03f9162ef341cc67218be40e22fe9808)
1 // RUN:  %clang_cc1 -std=c++2a -verify %s
2 
3 namespace functions
4 {
5   template<typename T>
6   struct S {
foofunctions::S7   static void foo(int) requires false {}
8   // expected-note@-1 3{{because 'false' evaluated to false}}
barfunctions::S9   static void bar(int) requires true {}
10   };
11 
12   void a(int);
13   void a(double);
14 
baz()15   void baz() {
16     S<int>::foo(1); // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
17     S<int>::bar(1);
18     void (*p1)(int) = S<int>::foo; // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
19     void (*p3)(int) = S<int>::bar;
20     decltype(S<int>::foo)* a1 = nullptr; // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
21     decltype(S<int>::bar)* a2 = nullptr;
22   }
23 }
24 
25 namespace methods
26 {
27   template<typename T>
28   struct A {
foomethods::A29     static void foo(int) requires (sizeof(T) == 1) {} // expected-note 3{{because 'sizeof(char[2]) == 1' (2 == 1) evaluated to false}}
barmethods::A30     static void bar(int) requires (sizeof(T) == 2) {} // expected-note 3{{because 'sizeof(char) == 2' (1 == 2) evaluated to false}}
31     // Make sure the function body is not instantiated before constraints are checked.
bazmethods::A32     static auto baz(int) requires (sizeof(T) == 2) { return T::foo(); } // expected-note{{because 'sizeof(char) == 2' (1 == 2) evaluated to false}}
33   };
34 
baz()35   void baz() {
36     A<char>::foo(1);
37     A<char>::bar(1); // expected-error{{invalid reference to function 'bar': constraints not satisfied}}
38     A<char>::baz(1); // expected-error{{invalid reference to function 'baz': constraints not satisfied}}
39     A<char[2]>::foo(1); // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
40     A<char[2]>::bar(1);
41     void (*p1)(int) = A<char>::foo;
42     void (*p2)(int) = A<char>::bar; // expected-error{{invalid reference to function 'bar': constraints not satisfied}}
43     void (*p3)(int) = A<char[2]>::foo; // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
44     void (*p4)(int) = A<char[2]>::bar;
45     decltype(A<char>::foo)* a1 = nullptr;
46     decltype(A<char>::bar)* a2 = nullptr; // expected-error{{invalid reference to function 'bar': constraints not satisfied}}
47     decltype(A<char[2]>::foo)* a3 = nullptr; // expected-error{{invalid reference to function 'foo': constraints not satisfied}}
48     decltype(A<char[2]>::bar)* a4 = nullptr;
49   }
50 }
51 
52 namespace operators
53 {
54   template<typename T>
55   struct A {
operator -operators::A56     A<T> operator-(A<T> b) requires (sizeof(T) == 1) { return b; } // expected-note{{because 'sizeof(int) == 1' (4 == 1) evaluated to false}}
57   };
58 
baz()59   void baz() {
60     auto* x = &A<int>::operator-; // expected-error{{invalid reference to function 'operator-': constraints not satisfied}}
61     auto y = &A<char>::operator-;
62   }
63 }
64