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 
foo()66   int foo() requires AtLeast2<long> && AtMost8<long> {
67     return 0;
68   }
69 
foo()70   double foo() requires AtLeast2<long> {
71     return 0.0;
72   }
73 
baz()74   double baz() requires AtLeast2<long> && AtMost8<long> { // expected-note {{candidate function}}
75     return 0.0;
76   }
77 
baz()78   int baz() requires AtMost8<long> && AtLeast2<long> { // expected-note {{candidate function}}
79     return 0.0;
80   }
81 
bar()82   void bar() requires (sizeof(char[8]) >= 8) { }
83   // expected-note@-1 {{candidate function}}
84   // expected-note@-2 {{similar constraint expressions not considered equivalent}}
85 
bar()86   void bar() requires (sizeof(char[8]) >= 8 && sizeof(int) <= 30) { }
87   // expected-note@-1 {{candidate function}}
88   // expected-note@-2 {{similar constraint expression here}}
89 
90   static_assert(is_same_v<decltype(foo()), int>);
91   static_assert(is_same_v<decltype(baz()), int>); // expected-error {{call to 'baz' is ambiguous}}
92   static_assert(is_same_v<decltype(bar()), void>); // expected-error {{call to 'bar' is ambiguous}}
93 
goo(int a)94   constexpr int goo(int a) requires AtLeast2<int> && true {
95     return 1;
96   }
97 
goo(const int b)98   constexpr int goo(const int b) requires AtLeast2<int> {
99     return 2;
100   }
101 
102   // Only trailing requires clauses of redeclarations are compared for overload resolution.
doo(int a,...)103   constexpr int doo(int a, ...) requires AtLeast2<int> && true { // expected-note {{candidate function}}
104     return 1;
105   }
106 
doo(int b)107   constexpr int doo(int b) requires AtLeast2<int> { // expected-note {{candidate function}}
108     return 2;
109   }
110 
111   static_assert(goo(1) == 1);
112   static_assert(doo(2) == 1); // expected-error {{call to 'doo' is ambiguous}}
113 }
114