1 // RUN: %clang_cc1 -std=c++1z -verify %s -DERRORS -Wundefined-func-template 2 // RUN: %clang_cc1 -std=c++1z -verify %s -UERRORS -Wundefined-func-template 3 4 // This test is split into two because we only produce "undefined internal" 5 // warnings if we didn't produce any errors. 6 #if ERRORS 7 8 namespace std { 9 using size_t = decltype(sizeof(0)); 10 template<typename T> struct initializer_list { 11 const T *p; 12 size_t n; 13 initializer_list(); 14 }; 15 // FIXME: This should probably not be necessary. 16 template<typename T> initializer_list(initializer_list<T>) -> initializer_list<T>; 17 } 18 19 template<typename T> constexpr bool has_type(...) { return false; } 20 template<typename T> constexpr bool has_type(T&) { return true; } 21 22 std::initializer_list il = {1, 2, 3, 4, 5}; 23 24 template<typename T> struct vector { 25 template<typename Iter> vector(Iter, Iter); 26 vector(std::initializer_list<T>); 27 }; 28 29 template<typename T> vector(std::initializer_list<T>) -> vector<T>; 30 template<typename Iter> explicit vector(Iter, Iter) -> vector<typename Iter::value_type>; 31 template<typename T> explicit vector(std::size_t, T) -> vector<T>; 32 33 vector v1 = {1, 2, 3, 4}; 34 static_assert(has_type<vector<int>>(v1)); 35 36 struct iter { typedef char value_type; } it, end; 37 vector v2(it, end); 38 static_assert(has_type<vector<char>>(v2)); 39 40 vector v3(5, 5); 41 static_assert(has_type<vector<int>>(v3)); 42 43 vector v4 = {it, end}; 44 static_assert(has_type<vector<iter>>(v4)); 45 46 vector v5{it, end}; 47 static_assert(has_type<vector<iter>>(v5)); 48 49 template<typename ...T> struct tuple { tuple(T...); }; 50 template<typename ...T> explicit tuple(T ...t) -> tuple<T...>; // expected-note {{declared}} 51 // FIXME: Remove 52 template<typename ...T> tuple(tuple<T...>) -> tuple<T...>; 53 54 const int n = 4; 55 tuple ta = tuple{1, 'a', "foo", n}; 56 static_assert(has_type<tuple<int, char, const char*, int>>(ta)); 57 58 tuple tb{ta}; 59 static_assert(has_type<tuple<int, char, const char*, int>>(tb)); 60 61 // FIXME: This should be tuple<tuple<...>>; when the above guide is removed. 62 tuple tc = {ta}; 63 static_assert(has_type<tuple<int, char, const char*, int>>(tc)); 64 65 tuple td = {1, 2, 3}; // expected-error {{selected an explicit deduction guide}} 66 static_assert(has_type<tuple<int, char, const char*, int>>(td)); 67 68 // FIXME: This is a GCC extension for now; if CWG don't allow this, at least 69 // add a warning for it. 70 namespace new_expr { 71 tuple<int> *p = new tuple{0}; 72 tuple<float, float> *q = new tuple(1.0f, 2.0f); 73 } 74 75 namespace ambiguity { 76 template<typename T> struct A {}; 77 A(unsigned short) -> A<int>; // expected-note {{candidate}} 78 A(short) -> A<int>; // expected-note {{candidate}} 79 A a = 0; // expected-error {{ambiguous deduction for template arguments of 'A'}} 80 81 template<typename T> struct B {}; 82 template<typename T> B(T(&)(int)) -> B<int>; // expected-note {{candidate function [with T = int]}} 83 template<typename T> B(int(&)(T)) -> B<int>; // expected-note {{candidate function [with T = int]}} 84 int f(int); 85 B b = f; // expected-error {{ambiguous deduction for template arguments of 'B'}} 86 } 87 88 // FIXME: Revisit this once CWG decides if attributes, and [[deprecated]] in 89 // particular, should be permitted here. 90 namespace deprecated { 91 template<typename T> struct A { A(int); }; 92 [[deprecated]] A(int) -> A<void>; // expected-note {{marked deprecated here}} 93 A a = 0; // expected-warning {{'<deduction guide for A>' is deprecated}} 94 } 95 96 namespace dependent { 97 template<template<typename...> typename A> decltype(auto) a = A{1, 2, 3}; 98 static_assert(has_type<vector<int>>(a<vector>)); 99 static_assert(has_type<tuple<int, int, int>>(a<tuple>)); 100 101 struct B { 102 template<typename T> struct X { X(T); }; 103 X(int) -> X<int>; 104 template<typename T> using Y = X<T>; // expected-note {{template}} 105 }; 106 template<typename T> void f() { 107 typename T::X tx = 0; 108 typename T::Y ty = 0; // expected-error {{alias template 'Y' requires template arguments; argument deduction only allowed for class templates}} 109 } 110 template void f<B>(); // expected-note {{in instantiation of}} 111 112 template<typename T> struct C { C(T); }; 113 template<typename T> C(T) -> C<T>; 114 template<typename T> void g(T a) { 115 C b = 0; 116 C c = a; 117 using U = decltype(b); // expected-note {{previous}} 118 using U = decltype(c); // expected-error {{different types ('C<const char *>' vs 'C<int>')}} 119 } 120 void h() { 121 g(0); 122 g("foo"); // expected-note {{instantiation of}} 123 } 124 } 125 126 namespace look_into_current_instantiation { 127 template<typename U> struct Q {}; 128 template<typename T> struct A { 129 using U = T; 130 template<typename> using V = Q<A<T>::U>; 131 template<typename W = int> A(V<W>); 132 }; 133 A a = Q<float>(); // ok, can look through class-scope typedefs and alias 134 // templates, and members of the current instantiation 135 A<float> &r = a; 136 137 template<typename T> struct B { // expected-note {{could not match 'B<T>' against 'int'}} 138 struct X { 139 typedef T type; 140 }; 141 B(typename X::type); // expected-note {{couldn't infer template argument 'T'}} 142 }; 143 B b = 0; // expected-error {{no viable}} 144 145 // We should have a substitution failure in the immediate context of 146 // deduction when using the C(T, U) constructor (probably; core wording 147 // unclear). 148 template<typename T> struct C { 149 using U = typename T::type; 150 C(T, U); 151 }; 152 153 struct R { R(int); typedef R type; }; 154 C(...) -> C<R>; 155 156 C c = {1, 2}; 157 } 158 159 namespace nondeducible { 160 template<typename A, typename B> struct X {}; 161 162 template<typename A> // expected-note {{non-deducible template parameter 'A'}} 163 X() -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} 164 165 template<typename A> // expected-note {{non-deducible template parameter 'A'}} 166 X(typename X<A, int>::type) -> X<A, int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} 167 168 template<typename A = int, 169 typename B> // expected-note {{non-deducible template parameter 'B'}} 170 X(int) -> X<A, B>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} 171 172 template<typename A = int, 173 typename ...B> 174 X(float) -> X<A, B...>; // ok 175 176 template <typename> struct UnnamedTemplateParam {}; 177 template <typename> // expected-note {{non-deducible template parameter (anonymous)}} 178 UnnamedTemplateParam() -> UnnamedTemplateParam<int>; // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} 179 } 180 181 namespace default_args_from_ctor { 182 template <class A> struct S { S(A = 0) {} }; 183 S s(0); 184 185 template <class A> struct T { template<typename B> T(A = 0, B = 0) {} }; 186 T t(0, 0); 187 } 188 189 namespace transform_params { 190 template<typename T, T N, template<T (*v)[N]> typename U, T (*X)[N]> 191 struct A { 192 template<typename V, V M, V (*Y)[M], template<V (*v)[M]> typename W> 193 A(U<X>, W<Y>); 194 195 static constexpr T v = N; 196 }; 197 198 int n[12]; 199 template<int (*)[12]> struct Q {}; 200 Q<&n> qn; 201 A a(qn, qn); 202 static_assert(a.v == 12); 203 204 template<typename ...T> struct B { 205 template<T ...V> B(const T (&...p)[V]) { 206 constexpr int Vs[] = {V...}; 207 static_assert(Vs[0] == 3 && Vs[1] == 4 && Vs[2] == 4); 208 } 209 static constexpr int (*p)(T...) = (int(*)(int, char, char))nullptr; 210 }; 211 B b({1, 2, 3}, "foo", {'x', 'y', 'z', 'w'}); // ok 212 213 template<typename ...T> struct C { 214 template<T ...V, template<T...> typename X> 215 C(X<V...>); 216 }; 217 template<int...> struct Y {}; 218 C c(Y<0, 1, 2>{}); 219 220 template<typename ...T> struct D { 221 template<T ...V> D(Y<V...>); 222 }; 223 D d(Y<0, 1, 2>{}); 224 } 225 226 namespace variadic { 227 int arr3[3], arr4[4]; 228 229 // PR32673 230 template<typename T> struct A { 231 template<typename ...U> A(T, U...); 232 }; 233 A a(1, 2, 3); 234 235 template<typename T> struct B { 236 template<int ...N> B(T, int (&...r)[N]); 237 }; 238 B b(1, arr3, arr4); 239 240 template<typename T> struct C { 241 template<template<typename> typename ...U> C(T, U<int>...); 242 }; 243 C c(1, a, b); 244 245 template<typename ...U> struct X { 246 template<typename T> X(T, U...); 247 }; 248 X x(1, 2, 3); 249 250 template<int ...N> struct Y { 251 template<typename T> Y(T, int (&...r)[N]); 252 }; 253 Y y(1, arr3, arr4); 254 255 template<template<typename> typename ...U> struct Z { 256 template<typename T> Z(T, U<int>...); 257 }; 258 Z z(1, a, b); 259 } 260 261 namespace tuple_tests { 262 // The converting n-ary constructor appears viable, deducing T as an empty 263 // pack (until we check its SFINAE constraints). 264 namespace libcxx_1 { 265 template<class ...T> struct tuple { 266 template<class ...Args> struct X { static const bool value = false; }; 267 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u); 268 }; 269 tuple a = {1, 2, 3}; 270 } 271 272 // Don't get caught by surprise when X<...> doesn't even exist in the 273 // selected specialization! 274 namespace libcxx_2 { 275 template<class ...T> struct tuple { 276 template<class ...Args> struct X { static const bool value = false; }; 277 // Substitution into X<U...>::value succeeds but produces the 278 // value-dependent expression 279 // tuple<T...>::X<>::value 280 // FIXME: Is that the right behavior? 281 template<class ...U, bool Y = X<U...>::value> tuple(U &&...u); 282 }; 283 template <> class tuple<> {}; 284 tuple a = {1, 2, 3}; // expected-error {{excess elements in struct initializer}} 285 } 286 287 namespace libcxx_3 { 288 template<typename ...T> struct scoped_lock { 289 scoped_lock(T...); 290 }; 291 template<> struct scoped_lock<> {}; 292 scoped_lock l = {}; 293 } 294 } 295 296 namespace dependent { 297 template<typename T> struct X { 298 X(T); 299 }; 300 template<typename T> int Var(T t) { 301 X x(t); 302 return X(x) + 1; // expected-error {{invalid operands}} 303 } 304 template<typename T> int Cast(T t) { 305 return X(X(t)) + 1; // expected-error {{invalid operands}} 306 } 307 template<typename T> int New(T t) { 308 return X(new X(t)) + 1; // expected-error {{invalid operands}} 309 }; 310 template int Var(float); // expected-note {{instantiation of}} 311 template int Cast(float); // expected-note {{instantiation of}} 312 template int New(float); // expected-note {{instantiation of}} 313 template<typename T> int operator+(X<T>, int); 314 template int Var(int); 315 template int Cast(int); 316 template int New(int); 317 318 template<template<typename> typename Y> void test() { 319 Y(0); 320 new Y(0); 321 Y y(0); 322 } 323 template void test<X>(); 324 } 325 326 namespace injected_class_name { 327 template<typename T = void> struct A { 328 A(); 329 template<typename U> A(A<U>); 330 }; 331 A<int> a; 332 A b = a; 333 using T = decltype(a); 334 using T = decltype(b); 335 } 336 337 namespace member_guides { 338 // PR34520 339 template<class> 340 struct Foo { 341 template <class T> struct Bar { 342 Bar(...) {} 343 }; 344 Bar(int) -> Bar<int>; 345 }; 346 Foo<int>::Bar b = 0; 347 348 struct A { 349 template<typename T> struct Public; // expected-note {{declared public}} 350 Public(float) -> Public<float>; 351 protected: // expected-note {{declared protected by intervening access specifier}} 352 template<typename T> struct Protected; // expected-note 2{{declared protected}} 353 Protected(float) -> Protected<float>; 354 Public(int) -> Public<int>; // expected-error {{different access}} 355 private: // expected-note {{declared private by intervening access specifier}} 356 template<typename T> struct Private; // expected-note {{declared private}} 357 Protected(int) -> Protected<int>; // expected-error {{different access}} 358 public: // expected-note 2{{declared public by intervening access specifier}} 359 template<typename T> Public(T) -> Public<T>; 360 template<typename T> Protected(T) -> Protected<T>; // expected-error {{different access}} 361 template<typename T> Private(T) -> Private<T>; // expected-error {{different access}} 362 }; 363 } 364 365 namespace rdar41903969 { 366 template <class T> struct A {}; 367 template <class T> struct B; 368 template <class T> struct C { 369 C(A<T>&); 370 C(B<T>&); 371 }; 372 373 void foo(A<int> &a, B<int> &b) { 374 (void)C{b}; 375 (void)C{a}; 376 } 377 378 template<typename T> struct X { 379 X(std::initializer_list<T>) = delete; 380 X(const X&); 381 }; 382 383 template <class T> struct D : X<T> {}; 384 385 void bar(D<int>& d) { 386 (void)X{d}; 387 } 388 } 389 390 namespace rdar41330135 { 391 template <int> struct A {}; 392 template <class T> 393 struct S { 394 template <class U> 395 S(T a, U t, A<sizeof(t)>); 396 }; 397 template <class T> struct D { 398 D(T t, A<sizeof(t)>); 399 }; 400 int f() { 401 S s(0, 0, A<sizeof(int)>()); 402 D d(0, A<sizeof(int)>()); 403 } 404 405 namespace test_dupls { 406 template<unsigned long> struct X {}; 407 template<typename T> struct A { 408 A(T t, X<sizeof(t)>); 409 }; 410 A a(0, {}); 411 template<typename U> struct B { 412 B(U u, X<sizeof(u)>); 413 }; 414 B b(0, {}); 415 } 416 417 } 418 419 namespace no_crash_on_default_arg { 420 class A { 421 template <typename T> class B { 422 B(int c = 1); 423 }; 424 // This used to crash due to unparsed default arg above. The diagnostic could 425 // be improved, but the point of this test is to simply check we do not crash. 426 B(); // expected-error {{deduction guide declaration without trailing return type}} 427 }; 428 } // namespace no_crash_on_default_arg 429 430 #pragma clang diagnostic push 431 #pragma clang diagnostic warning "-Wctad-maybe-unsupported" 432 namespace test_implicit_ctad_warning { 433 434 template <class T> 435 struct Tag {}; 436 437 template <class T> 438 struct NoExplicit { // expected-note {{add a deduction guide to suppress this warning}} 439 NoExplicit(T) {} 440 NoExplicit(T, int) {} 441 }; 442 443 // expected-warning@+1 {{'NoExplicit' may not intend to support class template argument deduction}} 444 NoExplicit ne(42); 445 446 template <class U> 447 struct HasExplicit { 448 HasExplicit(U) {} 449 HasExplicit(U, int) {} 450 }; 451 template <class U> HasExplicit(U, int) -> HasExplicit<Tag<U>>; 452 453 HasExplicit he(42); 454 455 // Motivating examples from (taken from Stephan Lavavej's 2018 Cppcon talk) 456 template <class T, class U> 457 struct AmateurPair { // expected-note {{add a deduction guide to suppress this warning}} 458 T first; 459 U second; 460 explicit AmateurPair(const T &t, const U &u) {} 461 }; 462 // expected-warning@+1 {{'AmateurPair' may not intend to support class template argument deduction}} 463 AmateurPair p1(42, "hello world"); // deduces to Pair<int, char[12]> 464 465 template <class T, class U> 466 struct AmateurPair2 { // expected-note {{add a deduction guide to suppress this warning}} 467 T first; 468 U second; 469 explicit AmateurPair2(T t, U u) {} 470 }; 471 // expected-warning@+1 {{'AmateurPair2' may not intend to support class template argument deduction}} 472 AmateurPair2 p2(42, "hello world"); // deduces to Pair2<int, const char*> 473 474 template <class T, class U> 475 struct ProPair { 476 T first; U second; 477 explicit ProPair(T const& t, U const& u) {} 478 }; 479 template<class T1, class T2> 480 ProPair(T1, T2) -> ProPair<T1, T2>; 481 ProPair p3(42, "hello world"); // deduces to ProPair<int, const char*> 482 static_assert(__is_same(decltype(p3), ProPair<int, const char*>)); 483 484 // Test that user-defined explicit guides suppress the warning even if they 485 // aren't used as candidates. 486 template <class T> 487 struct TestExplicitCtor { 488 TestExplicitCtor(T) {} 489 }; 490 template <class T> 491 explicit TestExplicitCtor(TestExplicitCtor<T> const&) -> TestExplicitCtor<void>; 492 TestExplicitCtor<int> ce1{42}; 493 TestExplicitCtor ce2 = ce1; 494 static_assert(__is_same(decltype(ce2), TestExplicitCtor<int>), ""); 495 496 struct allow_ctad_t { 497 allow_ctad_t() = delete; 498 }; 499 500 template <class T> 501 struct TestSuppression { 502 TestSuppression(T) {} 503 }; 504 TestSuppression(allow_ctad_t)->TestSuppression<void>; 505 TestSuppression ta("abc"); 506 static_assert(__is_same(decltype(ta), TestSuppression<const char *>), ""); 507 } 508 #pragma clang diagnostic pop 509 510 namespace PR41549 { 511 512 template <class H, class P> struct umm; 513 514 template <class H = int, class P = int> 515 struct umm { 516 umm(H h = 0, P p = 0); 517 }; 518 519 template <class H, class P> struct umm; 520 521 umm m(1); 522 523 } 524 525 namespace PR45124 { 526 class a { int d; }; 527 class b : a {}; 528 529 struct x { ~x(); }; 530 template<typename> class y { y(x = x()); }; 531 template<typename z> y(z)->y<z>; 532 533 // Not a constant initializer, but trivial default initialization. We won't 534 // detect this as trivial default initialization if synthesizing the implicit 535 // deduction guide 'template<typename T> y(x = x()) -> Y<T>;' leaves behind a 536 // pending cleanup. 537 __thread b g; 538 } 539 540 namespace PR47175 { 541 template<typename T> struct A { A(T); T x; }; 542 template<typename T> int &&n = A(T()).x; 543 int m = n<int>; 544 } 545 546 // Ensure we don't crash when CTAD fails. 547 template <typename T1, typename T2> 548 struct Foo { // expected-note{{candidate function template not viable}} 549 Foo(T1, T2); // expected-note{{candidate function template not viable}} 550 }; 551 552 template <typename... Args> 553 void insert(Args &&...args); 554 555 void foo() { 556 insert(Foo(2, 2, 2)); // expected-error{{no viable constructor or deduction guide}} 557 } 558 #else 559 560 // expected-no-diagnostics 561 namespace undefined_warnings { 562 // Make sure we don't get an "undefined but used internal symbol" warning for the deduction guide here. 563 namespace { 564 template <typename T> 565 struct TemplDObj { 566 explicit TemplDObj(T func) noexcept {} 567 }; 568 auto test1 = TemplDObj(0); 569 570 TemplDObj(float) -> TemplDObj<double>; 571 auto test2 = TemplDObj(.0f); 572 } 573 } 574 #endif 575