xref: /llvm-project/clang/test/CXX/temp/temp.constr/temp.constr.order/function-templates.cpp (revision 09e7fe9859b4efeeb35697c095c9676e993fc03c)
1 // RUN: %clang_cc1 -std=c++2a -x c++ -verify %s
2 
3 template<typename T> requires (sizeof(T) >= 4)
4 // expected-note@-1{{similar constraint expressions not considered equivalent}}
a()5 bool a() { return false; } // expected-note {{candidate function [with T = unsigned int]}}
6 
7 template<typename T> requires (sizeof(T) >= 4 && sizeof(T) <= 10)
8 // expected-note@-1{{similar constraint expression here}}
a()9 bool a() { return true; } // expected-note {{candidate function [with T = unsigned int]}}
10 
11 bool av = a<unsigned>(); // expected-error {{call to 'a' is ambiguous}}
12 
13 template<typename T>
14 concept C1 = sizeof(T) >= 4;
15 
16 template<typename T> requires C1<T>
b()17 constexpr bool b() { return false; }
18 
19 template<typename T> requires (C1<T> && sizeof(T) <= 10)
b()20 constexpr bool b() { return true; }
21 
22 static_assert(b<int>());
23 static_assert(!b<int[10]>());
24 
25 template<typename T>
26 concept C2 = sizeof(T) > 1 && sizeof(T) <= 8;
27 
28 template<typename T>
c()29 bool c() { return false; }
30 
31 template<typename T> requires C1<T>
c()32 bool c() { return true; }
33 
34 template<typename T> requires C1<T>
d()35 constexpr bool d() { return false; }
36 
37 template<typename T>
d()38 constexpr bool d() { return true; }
39 
40 static_assert(!d<int>());
41 
42 template<typename T>
e()43 constexpr int e() { return 1; }
44 
45 template<typename T> requires C1<T> && C2<T>
e()46 constexpr int e() { return 2; }
47 
48 template<typename T> requires C1<T> || C2<T>
49 constexpr int e() { return 3; }
50 
51 static_assert(e<unsigned>() == 2);
52 static_assert(e<char[10]>() == 3);
53 static_assert(e<char>() == 1);
54 
55 template<class T, class U>
56 concept BiggerThan = sizeof(T) > sizeof(U);
57 
58 template<class T>
59 concept BiggerThanInt = BiggerThan<T, int>;
60 
61 template<class T, class U> requires BiggerThan<T, U>
f()62 void f() { }
63 // expected-note@-1 {{candidate function [with T = long long, U = int]}}
64 
65 template<class T, class U> requires BiggerThanInt<T>
f()66 void f() { }
67 // expected-note@-1 {{candidate function [with T = long long, U = int]}}
68 
69 static_assert(sizeof(f<long long, int>()));
70 // expected-error@-1 {{call to 'f' is ambiguous}} \
71    expected-error@-1 {{invalid application of 'sizeof' to an incomplete type 'void'}}
72 
73 template<typename T>
74 concept C3 = true;
75 
76 template<typename T>
77 concept C4 = true && C3<T>;
78 
79 template<typename T> requires C3<void>
g()80 int g() { }
81 
82 template<typename T> requires C4<void>
g()83 int g() { }
84 
85 static_assert(sizeof(g<int>()));
86 
87 // Regression - used template parameter detection when only first out of
88 // multiple parameters are used
89 template <unsigned> struct X {};
90 template <class...> int h(X<0>);
91 template <unsigned b, class...> int h(X<b>);
92 static_assert(sizeof(h(X<0>{})));
93