1 // RUN: %clang_cc1 -std=c++2a -verify %s 2 template<typename T, typename U> constexpr bool is_same_v = false; 3 template<typename T> constexpr bool is_same_v<T, T> = true; 4 5 template<typename... T> 6 struct type_list; 7 8 namespace unconstrained { f1(auto x)9 decltype(auto) f1(auto x) { return x; } 10 static_assert(is_same_v<decltype(f1(1)), int>); 11 static_assert(is_same_v<decltype(f1('c')), char>); 12 f2(auto & x)13 decltype(auto) f2(auto &x) { return x; } 14 // expected-note@-1{{candidate function [with x:auto = int] not viable: expects an lvalue for 1st argument}} 15 // expected-note@-2{{candidate function [with x:auto = char] not viable: expects an lvalue for 1st argument}} 16 static_assert(is_same_v<decltype(f2(1)), int &>); // expected-error{{no matching}} 17 static_assert(is_same_v<decltype(f2('c')), char &>); // expected-error{{no matching}} 18 f3(const auto & x)19 decltype(auto) f3(const auto &x) { return x; } 20 static_assert(is_same_v<decltype(f3(1)), const int &>); 21 static_assert(is_same_v<decltype(f3('c')), const char &>); 22 f4(auto (* x)(auto y))23 decltype(auto) f4(auto (*x)(auto y)) { return x; } // expected-error{{'auto' not allowed in function prototype}} 24 f5(void (* x)(decltype(auto) y))25 decltype(auto) f5(void (*x)(decltype(auto) y)) { return x; } // expected-error{{'decltype(auto)' not allowed in function prototype}} 26 27 int return_int(); void return_void(); int foo(int); 28 f6(auto (* x)())29 decltype(auto) f6(auto (*x)()) { return x; } 30 // expected-note@-1{{candidate template ignored: failed template argument deduction}} 31 static_assert(is_same_v<decltype(f6(return_int)), int (*)()>); 32 static_assert(is_same_v<decltype(f6(return_void)), void (*)()>); 33 using f6c1 = decltype(f6(foo)); // expected-error{{no matching}} 34 35 decltype(auto) f7(auto (*x)() -> int) { return x; } 36 // expected-note@-1{{candidate function not viable: no known conversion from 'void ()' to 'auto (*)() -> int' for 1st argument}} 37 // expected-note@-2{{candidate function not viable: no known conversion from 'int (int)' to 'auto (*)() -> int' for 1st argument}} 38 static_assert(is_same_v<decltype(f7(return_int)), int (*)()>); 39 using f7c1 = decltype(f7(return_void)); // expected-error{{no matching}} 40 using f7c2 = decltype(f7(foo)); // expected-error{{no matching}} 41 static_assert(is_same_v<decltype(&f7), int (*(*)(int (*x)()))()>); 42 f8(auto...x)43 decltype(auto) f8(auto... x) { return (x + ...); } 44 static_assert(is_same_v<decltype(f8(1, 2, 3)), int>); 45 static_assert(is_same_v<decltype(f8('c', 'd')), int>); 46 static_assert(is_same_v<decltype(f8('c', 1)), int>); 47 f9(auto &...x)48 decltype(auto) f9(auto &... x) { return (x, ...); } 49 // expected-note@-1{{candidate function [with x:auto = <int (), int>] not viable: expects an lvalue for 2nd argument}} 50 using f9c1 = decltype(f9(return_int, 1)); // expected-error{{no matching}} 51 f11(decltype(auto) x)52 decltype(auto) f11(decltype(auto) x) { return x; } // expected-error{{'decltype(auto)' not allowed in function prototype}} 53 54 template<typename T> 55 auto f12(auto x, T y) -> type_list<T, decltype(x)>; 56 static_assert(is_same_v<decltype(f12(1, 'c')), type_list<char, int>>); 57 static_assert(is_same_v<decltype(f12<char>(1, 'c')), type_list<char, int>>); 58 59 template<typename T> 60 auto f13(T x, auto y) -> type_list<T, decltype(y)>; 61 static_assert(is_same_v<decltype(f13(1, 'c')), type_list<int, char>>); 62 static_assert(is_same_v<decltype(f13<char>(1, 'c')), type_list<char, char>>); 63 64 template<typename T> 65 auto f14(auto y) -> type_list<T, decltype(y)>; 66 static_assert(is_same_v<decltype(f14<int>('c')), type_list<int, char>>); 67 static_assert(is_same_v<decltype(f14<int, char>('c')), type_list<int, char>>); 68 69 template<typename T, typename U> 70 auto f15(auto y, U u) -> type_list<T, U, decltype(y)>; 71 static_assert(is_same_v<decltype(f15<int>('c', nullptr)), type_list<int, decltype(nullptr), char>>); 72 static_assert(is_same_v<decltype(f15<int, decltype(nullptr)>('c', nullptr)), type_list<int, decltype(nullptr), char>>); 73 74 auto f16(auto x, auto y) -> type_list<decltype(x), decltype(y)>; 75 static_assert(is_same_v<decltype(f16('c', 1)), type_list<char, int>>); 76 static_assert(is_same_v<decltype(f16<int>('c', 1)), type_list<int, int>>); 77 static_assert(is_same_v<decltype(f16<int, char>('c', 1)), type_list<int, char>>); 78 79 void f17(auto x, auto y) requires (sizeof(x) > 1); 80 // expected-note@-1{{candidate template ignored: constraints not satisfied [with x:auto = char, y:auto = int]}} 81 // expected-note@-2{{because 'sizeof (x) > 1' (1 > 1) evaluated to false}} 82 static_assert(is_same_v<decltype(f17('c', 1)), void>); 83 // expected-error@-1{{no matching}} 84 static_assert(is_same_v<decltype(f17<int>('c', 1)), void>); 85 static_assert(is_same_v<decltype(f17<int, char>('c', 1)), void>); 86 87 void f18(auto... x) requires (sizeof...(x) == 2); 88 // expected-note@-1{{candidate template ignored: constraints not satisfied [with x:auto = <char, int, int>]}} 89 // expected-note@-2{{candidate template ignored: constraints not satisfied [with x:auto = <char>]}} 90 // expected-note@-3{{because 'sizeof...(x) == 2' (1 == 2) evaluated to false}} 91 // expected-note@-4{{because 'sizeof...(x) == 2' (3 == 2) evaluated to false}} 92 static_assert(is_same_v<decltype(f18('c')), void>); 93 // expected-error@-1{{no matching}} 94 static_assert(is_same_v<decltype(f18('c', 1)), void>); 95 static_assert(is_same_v<decltype(f18('c', 1, 2)), void>); 96 // expected-error@-1{{no matching}} 97 98 template<typename T> 99 struct S { 100 constexpr auto f1(auto x, T t) -> decltype(x + t); 101 102 template<typename U> 103 constexpr auto f2(U u, auto x, T t) -> decltype(x + u + t); 104 }; 105 106 template<typename T> f1(auto x,T t)107 constexpr auto S<T>::f1(auto x, T t) -> decltype(x + t) { return x + t; } 108 109 template<typename T> 110 template<typename U> f2(auto x,U u,T t)111 constexpr auto S<T>::f2(auto x, U u, T t) -> decltype(x + u + t) { return x + u + t; } 112 // expected-error@-1 {{out-of-line definition of 'f2' does not match any declaration in 'S<T>'}} 113 114 template<typename T> 115 template<typename U> f2(U u,auto x,T t)116 constexpr auto S<T>::f2(U u, auto x, T t) -> decltype(x + u + t) { return x + u + t; } 117 118 template<> 119 template<> f2(double u,char x,int t)120 constexpr auto S<int>::f2<double>(double u, char x, int t) -> double { return 42; } 121 122 static_assert(S<char>{}.f1(1, 2) == 3); 123 static_assert(S<char>{}.f2(1, 2, '\x00') == 3); 124 static_assert(S<char>{}.f2<double>(1, 2, '\x00') == 3.); 125 static_assert(S<int>{}.f2<double>(1, '2', '\x00') == 42); 126 } 127 128 namespace constrained { 129 template<typename T> 130 concept C = is_same_v<T, int>; 131 // expected-note@-1 12{{because}} 132 template<typename T, typename U> 133 concept C2 = is_same_v<T, U>; 134 // expected-note@-1 12{{because}} 135 136 int i; 137 const int ci = 1; 138 char c; 139 const char cc = 'a'; 140 int g(int); 141 char h(int); 142 143 void f1(C auto x); 144 // expected-note@-1 {{candidate template ignored: constraints not satisfied [with x:auto = }} 145 // expected-note@-2{{because}} 146 static_assert(is_same_v<decltype(f1(1)), void>); 147 static_assert(is_same_v<decltype(f1('a')), void>); 148 // expected-error@-1{{no matching}} 149 void f2(C auto &x); 150 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 151 static_assert(is_same_v<decltype(f2(i)), void>); 152 static_assert(is_same_v<decltype(f2(ci)), void>); 153 // expected-error@-1{{no matching}} 154 static_assert(is_same_v<decltype(f2(c)), void>); 155 // expected-error@-1{{no matching}} 156 void f3(const C auto &x); 157 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 158 static_assert(is_same_v<decltype(f3(i)), void>); 159 static_assert(is_same_v<decltype(f3(ci)), void>); 160 static_assert(is_same_v<decltype(f3(c)), void>); 161 // expected-error@-1{{no matching}} 162 void f4(C auto (*x)(C auto y)); // expected-error{{'auto' not allowed}} 163 void f5(C auto (*x)(int y)); 164 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 165 static_assert(is_same_v<decltype(f5(g)), void>); 166 static_assert(is_same_v<decltype(f5(h)), void>); 167 // expected-error@-1{{no matching}} 168 void f6(C auto (*x)() -> int); // expected-error{{function with trailing return type must specify return type 'auto', not 'C auto'}} 169 void f7(C auto... x); 170 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 171 static_assert(is_same_v<decltype(f7(1, 2)), void>); 172 static_assert(is_same_v<decltype(f7(1, 'a')), void>); 173 // expected-error@-1{{no matching}} 174 static_assert(is_same_v<decltype(f7('a', 2)), void>); 175 // expected-error@-1{{no matching}} 176 void f8(C auto &... x); 177 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 178 static_assert(is_same_v<decltype(f8(i, i)), void>); 179 static_assert(is_same_v<decltype(f8(i, c)), void>); 180 // expected-error@-1{{no matching}} 181 static_assert(is_same_v<decltype(f8(i, i, ci)), void>); 182 // expected-error@-1{{no matching}} 183 void f9(const C auto &... x); 184 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 185 static_assert(is_same_v<decltype(f9(i, i)), void>); 186 static_assert(is_same_v<decltype(f9(i, c)), void>); 187 // expected-error@-1{{no matching}} 188 static_assert(is_same_v<decltype(f9(i, i, ci)), void>); 189 void f10(C decltype(auto) x); __anon61b486030102(C auto x) 190 auto f11 = [] (C auto x) { }; 191 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 192 static_assert(is_same_v<decltype(f11(1)), void>); 193 static_assert(is_same_v<decltype(f11('a')), void>); 194 // expected-error@-1{{no matching}} 195 196 void f12(C2<char> auto x); 197 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 198 static_assert(is_same_v<decltype(f12(1)), void>); 199 // expected-error@-1{{no matching}} 200 static_assert(is_same_v<decltype(f12('a')), void>); 201 void f13(C2<char> auto &x); 202 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 203 static_assert(is_same_v<decltype(f13(i)), void>); 204 // expected-error@-1{{no matching}} 205 static_assert(is_same_v<decltype(f13(cc)), void>); 206 // expected-error@-1{{no matching}} 207 static_assert(is_same_v<decltype(f13(c)), void>); 208 void f14(const C2<char> auto &x); 209 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 210 static_assert(is_same_v<decltype(f14(i)), void>); 211 // expected-error@-1{{no matching}} 212 static_assert(is_same_v<decltype(f14(cc)), void>); 213 static_assert(is_same_v<decltype(f14(c)), void>); 214 void f15(C2<char> auto (*x)(C2<int> auto y)); // expected-error{{'auto' not allowed}} 215 void f16(C2<char> auto (*x)(int y)); 216 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 217 static_assert(is_same_v<decltype(f16(g)), void>); 218 // expected-error@-1{{no matching}} 219 static_assert(is_same_v<decltype(f16(h)), void>); 220 void f17(C2<char> auto (*x)() -> int); // expected-error{{function with trailing return type must specify return type 'auto', not 'C2<char> auto'}} 221 void f18(C2<char> auto... x); 222 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 223 static_assert(is_same_v<decltype(f18('a', 'b')), void>); 224 static_assert(is_same_v<decltype(f18('a', 1)), void>); 225 // expected-error@-1{{no matching}} 226 static_assert(is_same_v<decltype(f18(2, 'a')), void>); 227 // expected-error@-1{{no matching}} 228 void f19(C2<char> auto &... x); 229 // expected-note@-1 2{{candidate template ignored}} expected-note@-1 2{{because}} 230 static_assert(is_same_v<decltype(f19(c, c)), void>); 231 static_assert(is_same_v<decltype(f19(i, c)), void>); 232 // expected-error@-1{{no matching}} 233 static_assert(is_same_v<decltype(f19(c, c, cc)), void>); 234 // expected-error@-1{{no matching}} 235 void f20(const C2<char> auto &... x); 236 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 237 static_assert(is_same_v<decltype(f20(c, c)), void>); 238 static_assert(is_same_v<decltype(f20(i, c)), void>); 239 // expected-error@-1{{no matching}} 240 static_assert(is_same_v<decltype(f20(c, c, cc)), void>); 241 void f21(C2<char> decltype(auto) x); __anon61b486030202(C2<char> auto x) 242 auto f22 = [] (C2<char> auto x) { }; 243 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 244 static_assert(is_same_v<decltype(f22(1)), void>); 245 // expected-error@-1{{no matching}} 246 static_assert(is_same_v<decltype(f22('a')), void>); 247 248 struct S1 { S1(C auto); }; 249 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 250 // expected-note@-2 2{{candidate constructor}} 251 static_assert(is_same_v<decltype(S1(1)), S1>); 252 static_assert(is_same_v<decltype(S1('a')), S1>); 253 // expected-error@-1{{no matching}} 254 struct S2 { S2(C2<char> auto); }; 255 // expected-note@-1{{candidate template ignored}} expected-note@-1{{because}} 256 // expected-note@-2 2{{candidate constructor}} 257 static_assert(is_same_v<decltype(S2(1)), S2>); 258 // expected-error@-1{{no matching}} 259 static_assert(is_same_v<decltype(S2('a')), S2>); 260 } 261