xref: /llvm-project/clang/test/CXX/over/over.match/over.match.best/p1-2a.cpp (revision b56b3d75b04cda762ac8e15b7ec6fa4f52a6735a)
1 // RUN:  %clang_cc1 -std=c++2a -verify %s
2 
3 template<typename T, typename U>
4 constexpr static bool is_same_v = false;
5 
6 template<typename T>
7 constexpr static bool is_same_v<T, T> = true;
8 
9 namespace templates
10 {
11   template<typename T>
12   concept AtLeast1 = sizeof(T) >= 1;
13 
14   template<typename T>
foo(T t)15   int foo(T t) requires (sizeof(T) == 4) { // expected-note {{candidate function}}
16     return 0;
17   }
18 
19   template<typename T>
foo(T t)20   char foo(T t) requires AtLeast1<T> { // expected-note {{candidate function}}
21     return 'a';
22   }
23 
24   template<typename T>
foo(T t)25   double foo(T t) requires (AtLeast1<T> && sizeof(T) <= 2) {
26     return 'a';
27   }
28 
29   static_assert(is_same_v<decltype(foo(10)), int>); // expected-error {{call to 'foo' is ambiguous}}
30   static_assert(is_same_v<decltype(foo(short(10))), double>);
31 
32   template<typename T>
bar()33   void bar() requires (sizeof(T) == 1) { }
34   // expected-note@-1{{similar constraint expressions not considered equivalent}}
35   // expected-note@-2{{candidate function [with T = char]}}
36 
37   template<typename T>
bar()38   void bar() requires (sizeof(T) == 1 && sizeof(T) >= 0) { }
39   // expected-note@-1{{candidate function [with T = char]}}
40   // expected-note@-2{{similar constraint expression here}}
41 
42   static_assert(is_same_v<decltype(bar<char>()), void>);
43   // expected-error@-1{{call to 'bar' is ambiguous}}
44 
45   template<typename T>
baz()46   constexpr int baz() requires AtLeast1<T> { // expected-note {{candidate function}}
47     return 1;
48   }
49 
50   template<typename T> requires AtLeast1<T>
baz()51   constexpr int baz() { // expected-note {{candidate function [with T = int]}}
52     return 2;
53   }
54 
55   static_assert(baz<int>() == 1); // expected-error {{call to 'baz' is ambiguous}}
56 }
57 
58 namespace non_template
59 {
60   template<typename T>
61   concept AtLeast2 = sizeof(T) >= 2;
62 
63   template<typename T>
64   concept AtMost8 = sizeof(T) <= 8;
65 
66   template<typename T>
foo()67   int foo() requires AtLeast2<long> && AtMost8<long> {
68     return 0;
69   }
70 
71   template<typename T>
foo()72   double foo() requires AtLeast2<long> {
73     return 0.0;
74   }
75 
76   template<typename T>
baz()77   double baz() requires AtLeast2<long> && AtMost8<long> { // expected-note {{candidate function}}
78     return 0.0;
79   }
80 
81   template<typename T>
baz()82   int baz() requires AtMost8<long> && AtLeast2<long> { // expected-note {{candidate function}}
83     return 0.0;
84   }
85 
86   template<typename T>
bar()87   void bar() requires (sizeof(char[8]) >= 8) { }
88   // expected-note@-1 {{candidate function}}
89   // expected-note@-2 {{similar constraint expressions not considered equivalent}}
90 
91   template<typename T>
bar()92   void bar() requires (sizeof(char[8]) >= 8 && sizeof(int) <= 30) { }
93   // expected-note@-1 {{candidate function}}
94   // expected-note@-2 {{similar constraint expression here}}
95 
96   static_assert(is_same_v<decltype(foo<int>()), int>);
97   static_assert(is_same_v<decltype(baz<int>()), int>); // expected-error {{call to 'baz' is ambiguous}}
98   static_assert(is_same_v<decltype(bar<int>()), void>); // expected-error {{call to 'bar' is ambiguous}}
99 
100   // Top-level cv-qualifiers are ignored in template partial ordering per [dcl.fct]/p5.
101   //   After producing the list of parameter types, any top-level cv-qualifiers modifying
102   //   a parameter type are deleted when forming the function type.
103   template<typename T>
goo(T a)104   constexpr int goo(T a) requires AtLeast2<T> && true {
105     return 1;
106   }
107 
108   template<typename T>
goo(const T b)109   constexpr int goo(const T b) requires AtLeast2<T> {
110     return 2;
111   }
112 
113   // [temp.func.order] p5
114   //   Since, in a call context, such type deduction considers only parameters
115   //   for which there are explicit call arguments, some parameters are ignored
116   //   (namely, function parameter packs, parameters with default arguments, and
117   //   ellipsis parameters).
118   template<typename T>
doo(int a,...)119   constexpr int doo(int a, ...) requires AtLeast2<int> && true {
120     return 1;
121   }
122 
123   template<typename T>
doo(int b)124   constexpr int doo(int b) requires AtLeast2<int> {
125     return 2;
126   }
127 
128   static_assert(goo<int>(1) == 1);
129   static_assert(doo<int>(2) == 1);
130 }
131