1 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s 2 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING 3 4 auto f(); // expected-note {{previous}} 5 int f(); // expected-error {{differ only in their return type}} 6 7 auto &g(); 8 auto g() -> auto &; 9 10 auto h() -> auto *; 11 auto *h(); 12 13 struct Conv1 { 14 operator auto(); // expected-note {{declared here}} 15 } conv1; 16 int conv1a = conv1; // expected-error {{function 'operator auto' with deduced return type cannot be used before it is defined}} 17 // expected-error@-1 {{no viable conversion}} operator auto()18 Conv1::operator auto() { return 123; } 19 int conv1b = conv1; 20 int conv1c = conv1.operator auto(); 21 int conv1d = conv1.operator int(); // expected-error {{no member named 'operator int'}} 22 23 struct Conv2 { operator autoConv224 operator auto() { return 0; } // expected-note {{previous}} operator autoConv225 operator auto() { return 0.; } // expected-error {{cannot be redeclared}} expected-error {{cannot initialize return object of type 'auto' with an rvalue of type 'double'}} 26 }; 27 28 struct Conv3 { operator autoConv329 operator auto() { int *p = nullptr; return p; } // expected-note {{candidate}} operator auto*Conv330 operator auto*() { int *p = nullptr; return p; } // expected-note {{candidate}} 31 } conv3; 32 int *conv3a = conv3; // expected-error {{ambiguous}} 33 int *conv3b = conv3.operator auto(); 34 int *conv3c = conv3.operator auto*(); 35 36 template<typename T> 37 struct Conv4 { operator autoConv438 operator auto() { return T(); } 39 }; 40 Conv4<int> conv4int; 41 int conv4a = conv4int; 42 int conv4b = conv4int.operator auto(); 43 44 auto a(); a()45 auto a() { return 0; } 46 using T = decltype(a()); 47 using T = int; 48 auto a(); // expected-note {{previous}} 49 using T = decltype(a()); 50 auto *a(); // expected-error {{differ only in their return type}} 51 b(bool k)52 auto b(bool k) { 53 if (k) 54 return "hello"; 55 return "goodbye"; 56 } 57 ptr_1()58 auto *ptr_1() { 59 return 100; // expected-error {{cannot deduce return type 'auto *' from returned value of type 'int'}} 60 } 61 ref_1()62 const auto &ref_1() { 63 return 0; // expected-warning {{returning reference to local temporary}} 64 } 65 init_list()66 auto init_list() { 67 return { 1, 2, 3 }; // expected-error {{cannot deduce return type from initializer list}} 68 } 69 70 auto fwd_decl(); // expected-note 2{{here}} 71 72 int n = fwd_decl(); // expected-error {{function 'fwd_decl' with deduced return type cannot be used before it is defined}} 73 int k = sizeof(fwd_decl()); // expected-error {{used before it is defined}} 74 fac(int n)75 auto fac(int n) { 76 if (n <= 2) 77 return n; 78 return n * fac(n-1); // ok 79 } 80 fac_2(int n)81 auto fac_2(int n) { // expected-note {{declared here}} 82 if (n > 2) 83 return n * fac_2(n-1); // expected-error {{cannot be used before it is defined}} 84 return n; 85 } 86 void_ret()87 auto void_ret() {} 88 using Void = void; 89 using Void = decltype(void_ret()); 90 void_ret_2()91 auto &void_ret_2() {} // expected-error {{cannot deduce return type 'auto &' for function with no return statements}} void_ret_3()92 const auto void_ret_3() {} // ok, return type 'const void' is adjusted to 'void' 93 void_ret_4()94 const auto void_ret_4() { 95 if (false) 96 return void(); 97 if (false) 98 return; 99 return 0; // expected-error {{'auto' in return type deduced as 'int' here but deduced as 'void' in earlier return statement}} 100 } 101 102 namespace Templates { f1()103 template<typename T> auto f1() { 104 return T() + 1; 105 } f2(T && v)106 template<typename T> auto &f2(T &&v) { return v; } 107 int a = f1<int>(); 108 const int &b = f2(0); 109 double d; 110 float &c = f2(0.0); // expected-error {{non-const lvalue reference to type 'float' cannot bind to a value of unrelated type 'double'}} 111 112 template<typename T> auto fwd_decl(); // expected-note {{declared here}} 113 int e = fwd_decl<int>(); // expected-error {{cannot be used before it is defined}} fwd_decl()114 template<typename T> auto fwd_decl() { return 0; } 115 int f = fwd_decl<int>(); 116 template <typename T> 117 auto fwd_decl(); // expected-note {{candidate template ignored: could not match 'auto ()' against 'int ()'}} 118 int g = fwd_decl<char>(); 119 120 auto (*p)() = f1; // expected-error {{incompatible initializer}} 121 auto (*q)() = f1<int>; // ok 122 123 typedef decltype(f2(1.2)) dbl; // expected-note {{previous}} 124 typedef float dbl; // expected-error {{typedef redefinition with different types ('float' vs 'decltype(f2(1.2))' (aka 'double &'))}} 125 126 extern template auto fwd_decl<double>(); 127 int k1 = fwd_decl<double>(); 128 extern template int fwd_decl<char>(); // expected-error {{does not refer to a function template}} 129 int k2 = fwd_decl<char>(); 130 instantiate()131 template <typename T> auto instantiate() { T::error; } // expected-error {{has no members}} \ 132 // expected-note {{candidate template ignored: could not match 'auto ()' against 'void ()'}} 133 extern template auto instantiate<int>(); // ok 134 int k = instantiate<int>(); // expected-note {{in instantiation of}} instantiate()135 template<> auto instantiate<char>() {} // ok instantiate()136 template<> void instantiate<double>() {} // expected-error {{no function template matches}} 137 arg_single()138 template<typename T> auto arg_single() { return 0; } arg_multi()139 template<typename T> auto arg_multi() { return 0l; } arg_multi(int)140 template<typename T> auto arg_multi(int) { return "bad"; } 141 template<typename T> struct Outer { arg_singleTemplates::Outer142 static auto arg_single() { return 0.f; } arg_multiTemplates::Outer143 static auto arg_multi() { return 0.; } arg_multiTemplates::Outer144 static auto arg_multi(int) { return "bad"; } 145 }; 146 template<typename T> T &take_fn(T (*p)()); 147 148 int &check1 = take_fn(arg_single); // expected-error {{no matching}} expected-note@-2 {{couldn't infer}} 149 int &check2 = take_fn(arg_single<int>); 150 int &check3 = take_fn<int>(arg_single); // expected-error {{no matching}} expected-note@-4{{no overload of 'arg_single'}} 151 int &check4 = take_fn<int>(arg_single<int>); 152 long &check5 = take_fn(arg_multi); // expected-error {{no matching}} expected-note@-6 {{couldn't infer}} 153 long &check6 = take_fn(arg_multi<int>); 154 long &check7 = take_fn<long>(arg_multi); // expected-error {{no matching}} expected-note@-8{{no overload of 'arg_multi'}} 155 long &check8 = take_fn<long>(arg_multi<int>); 156 157 float &mem_check1 = take_fn(Outer<int>::arg_single); 158 float &mem_check2 = take_fn<float>(Outer<char>::arg_single); 159 double &mem_check3 = take_fn(Outer<long>::arg_multi); 160 double &mem_check4 = take_fn<double>(Outer<double>::arg_multi); 161 162 namespace Deduce1 { f()163 template <typename T> auto f() { return 0; } // expected-note {{couldn't infer template argument 'T'}} 164 template<typename T> void g(T(*)()); // expected-note 2{{candidate}} h()165 void h() { 166 auto p = f<int>; 167 auto (*q)() = f<int>; 168 int (*r)() = f; // expected-error {{does not match}} 169 g(f<int>); 170 g<int>(f); // expected-error {{no matching function}} 171 g(f); // expected-error {{no matching function}} 172 } 173 } 174 175 namespace Deduce2 { f(int)176 template <typename T> auto f(int) { return 0; } // expected-note {{couldn't infer template argument 'T'}} 177 template<typename T> void g(T(*)(int)); // expected-note 2{{candidate}} h()178 void h() { 179 auto p = f<int>; 180 auto (*q)(int) = f<int>; 181 int (*r)(int) = f; // expected-error {{does not match}} 182 g(f<int>); 183 g<int>(f); // expected-error {{no matching function}} 184 g(f); // expected-error {{no matching function}} 185 } 186 } 187 188 namespace Deduce3 { f(T)189 template<typename T> auto f(T) { return 0; } 190 template<typename T> void g(T(*)(int)); // expected-note {{couldn't infer}} h()191 void h() { 192 auto p = f<int>; 193 auto (*q)(int) = f<int>; 194 int (*r)(int) = f; // ok 195 g(f<int>); 196 g<int>(f); // ok 197 g(f); // expected-error {{no matching function}} 198 } 199 } 200 201 namespace DeduceInDeducedReturnType { f()202 template<typename T, typename U> auto f() -> auto (T::*)(U) { 203 int (T::*result)(U) = nullptr; 204 return result; 205 } 206 struct S {}; 207 int (S::*(*p)())(double) = f; 208 int (S::*(*q)())(double) = f<S, double>; 209 } 210 } 211 212 auto fwd_decl_using(); 213 namespace N { using ::fwd_decl_using; } fwd_decl_using()214 auto fwd_decl_using() { return 0; } 215 namespace N { int k = N::fwd_decl_using(); } 216 217 namespace OverloadResolutionNonTemplate { 218 auto f(); 219 auto f(int); // expected-note {{here}} 220 221 int &g(int (*f)()); // expected-note {{not viable: no overload of 'f' matching 'int (*)()'}} 222 char &g(int (*f)(int)); // expected-note {{not viable: no overload of 'f' matching 'int (*)(int)'}} 223 224 int a = g(f); // expected-error {{no matching function}} 225 f()226 auto f() { return 0; } 227 228 // FIXME: It's not completely clear whether this should be ill-formed. 229 int &b = g(f); // expected-error {{used before it is defined}} 230 f(int)231 auto f(int) { return 0.0; } 232 233 int &c = g(f); // ok 234 } 235 236 namespace OverloadResolutionTemplate { 237 auto f(); 238 template<typename T> auto f(T); 239 240 int &g(int (*f)()); // expected-note {{not viable: no overload of 'f' matching 'int (*)()'}} expected-note {{candidate}} 241 char &g(int (*f)(int)); // expected-note {{not viable: no overload of 'f' matching 'int (*)(int)'}} expected-note {{candidate}} 242 243 int a = g(f); // expected-error {{no matching function}} 244 f()245 auto f() { return 0; } 246 247 int &b = g(f); // ok (presumably), due to deduction failure forming type of 'f<int>' 248 f(T)249 template<typename T> auto f(T) { return 0; } 250 251 int &c = g(f); // expected-error {{ambiguous}} 252 } 253 254 namespace DefaultedMethods { 255 struct A { 256 auto operator=(const A&) = default; // expected-error {{must return 'DefaultedMethods::A &'}} 257 A &operator=(A&&); // expected-note {{previous}} 258 }; 259 auto A::operator=(A&&) = default; // expected-error {{return type of out-of-line definition of 'DefaultedMethods::A::operator=' differs from that in the declaration}} 260 } 261 262 namespace Constexpr { f1(int n)263 constexpr auto f1(int n) { return n; } fConstexpr::X264 template<typename T> struct X { constexpr auto f() {} }; // PR18746 fConstexpr::Y265 template<typename T> struct Y { constexpr T f() {} }; // expected-note {{control reached end of constexpr function}} f()266 void f() { 267 X<int>().f(); 268 Y<void>().f(); 269 constexpr int q = Y<int>().f(); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to '&Y<int>()->f()'}} 270 } 271 struct NonLiteral { ~NonLiteral(); } nl; // expected-note {{user-provided destructor}} f2(int n)272 constexpr auto f2(int n) { return nl; } // expected-error {{return type 'Constexpr::NonLiteral' is not a literal type}} 273 } 274 275 // It's not really clear whether these are valid, but this matches g++. 276 using size_t = decltype(sizeof(0)); 277 auto operator new(size_t n, const char*); // expected-error {{must return type 'void *'}} 278 auto operator delete(void *, const char*); // expected-error {{must return type 'void'}} 279 280 namespace Virtual { 281 struct S { fVirtual::S282 virtual auto f() { return 0; } // expected-error {{function with deduced return type cannot be virtual}} expected-note {{here}} 283 }; 284 // Allow 'auto' anyway for error recovery. 285 struct T : S { 286 int f(); 287 }; 288 struct U : S { 289 auto f(); // expected-error {{different return}} 290 }; 291 292 // And here's why... 293 struct V { virtual auto f(); }; // expected-error {{cannot be virtual}} 294 struct W : V { virtual auto f(); }; // expected-error {{cannot be virtual}} f()295 auto V::f() { return 0; } // in tu1.cpp f()296 auto W::f() { return 0.0; } // in tu2.cpp 297 W w; 298 int k1 = w.f(); 299 int k2 = ((V&)w).f(); 300 } 301 302 namespace std_examples { 303 304 namespace NoReturn { f()305 auto f() {} 306 void (*p)() = &f; 307 308 auto f(); // ok 309 g()310 auto *g() {} // expected-error {{cannot deduce return type 'auto *' for function with no return statements}} 311 312 auto h() = delete; // expected-note {{explicitly deleted}} 313 auto x = h(); // expected-error {{call to deleted}} 314 } 315 316 namespace UseBeforeComplete { 317 auto n = n; // expected-error {{variable 'n' declared with 'auto' type cannot appear in its own initializer}} 318 auto f(); // expected-note {{declared here}} g()319 void g() { &f; } // expected-error {{function 'f' with deduced return type cannot be used before it is defined}} sum(int i)320 auto sum(int i) { 321 if (i == 1) 322 return i; 323 else 324 return sum(i - 1) + i; 325 } 326 } 327 328 namespace Redecl { 329 auto f(); f()330 auto f() { return 42; } 331 auto f(); // expected-note 2{{previous}} 332 int f(); // expected-error {{functions that differ only in their return type cannot be overloaded}} 333 decltype(auto) f(); // expected-error {{cannot be overloaded}} 334 g(T t)335 template <typename T> auto g(T t) { return t; } // expected-note {{candidate}} \ 336 // expected-note {{candidate function [with T = int]}} 337 template auto g(int); 338 template char g(char); // expected-error {{does not refer to a function}} 339 template<> auto g(double); 340 g(T t)341 template<typename T> T g(T t) { return t; } // expected-note {{candidate}} 342 template char g(char); 343 template auto g(float); 344 h()345 void h() { return g(42); } // expected-error {{ambiguous}} 346 } 347 348 namespace ExplicitInstantiationDecl { f(T t)349 template<typename T> auto f(T t) { return t; } 350 extern template auto f(int); 351 int (*p)(int) = f; 352 } 353 namespace MemberTemplatesWithDeduction { 354 struct M { foostd_examples::MemberTemplatesWithDeduction::M355 template<class T> auto foo(T t) { return t; } operator ()std_examples::MemberTemplatesWithDeduction::M356 template<class T> auto operator()(T t) const { return t; } static_foostd_examples::MemberTemplatesWithDeduction::M357 template<class T> static __attribute__((unused)) int static_foo(T) { 358 return 5; 359 } operator Tstd_examples::MemberTemplatesWithDeduction::M360 template<class T> operator T() { return T{}; } operator autostd_examples::MemberTemplatesWithDeduction::M361 operator auto() { return &static_foo<int>; } 362 }; 363 struct N : M { 364 using M::foo; 365 using M::operator(); 366 using M::static_foo; 367 using M::operator auto; 368 }; 369 test()370 template <class T> int test() { 371 int i = T{}.foo(3); 372 T m = T{}.foo(M{}); 373 int j = T{}(3); 374 M m2 = M{}(M{}); 375 int k = T{}.static_foo(4); 376 int l = T::static_foo(5); 377 int l2 = T{}; 378 struct X { }; 379 X x = T{}; 380 return 0; 381 } 382 int Minst = test<M>(); 383 int Ninst = test<N>(); 384 385 } 386 } 387 388 namespace CurrentInstantiation { 389 // PR16875 390 template<typename T> struct S { fCurrentInstantiation::S391 auto f() { return T(); } gCurrentInstantiation::S392 int g() { return f(); } hCurrentInstantiation::S393 auto h(bool b) { 394 if (b) 395 return T(); 396 return h(true); 397 } 398 }; 399 int k1 = S<int>().g(); 400 int k2 = S<int>().h(false); 401 402 template<typename T> struct U { 403 #ifndef DELAYED_TEMPLATE_PARSING 404 auto f(); // expected-note {{here}} gCurrentInstantiation::U405 int g() { return f(); } // expected-error {{cannot be used before it is defined}} 406 #else 407 auto f(); 408 int g() { return f(); } 409 #endif 410 }; 411 #ifndef DELAYED_TEMPLATE_PARSING 412 template int U<int>::g(); // expected-note {{in instantiation of}} 413 #else 414 template int U<int>::g(); 415 #endif f()416 template<typename T> auto U<T>::f() { return T(); } 417 template int U<short>::g(); // ok 418 } 419 420 namespace WithDefaultArgs { 421 template<typename U> struct A { __anond37ef2410102null422 template<typename T = U> friend auto f(A) { return []{}; } 423 }; 424 template<typename T> void f(); 425 using T = decltype(f(A<int>())); 426 using T = decltype(f<int>(A<int>())); 427 } 428 429 namespace MultilevelDeduction { 430 F()431 auto F() -> auto* { return (int*)0; } 432 G()433 auto (*G())() -> int* { return F; } 434 435 auto run = G(); 436 437 namespace Templated { 438 template<class T> F(T t)439 auto F(T t) -> auto* { return (T*)0; } 440 441 template<class T> G(T t)442 auto (*G(T t))(T) -> T* { return &F<T>; } 443 444 445 template<class T> G2(T t)446 auto (*G2(T t))(T) -> auto* { return &F<T>; } 447 448 auto run_int = G(1); 449 auto run_char = G2('a'); 450 451 } 452 } 453 454 namespace rnk { 455 extern "C" int puts(const char *s); 456 template <typename T> foo(T x)457 auto foo(T x) -> decltype(x) { 458 #ifdef DELAYED_TEMPLATE_PARSING 459 ::rnk::bar(); 460 #endif 461 return x; 462 } bar()463 void bar() { puts("bar"); } main()464 int main() { return foo(0); } 465 466 } 467 468 namespace OverloadedOperators { 469 template<typename T> struct A { operator ()OverloadedOperators::A470 auto operator()() { return T{}; } operator []OverloadedOperators::A471 auto operator[](int) { return T{}; } operator +OverloadedOperators::A472 auto operator+(int) { return T{}; } operator +OverloadedOperators::A473 auto operator+() { return T{}; } operator -(A)474 friend auto operator-(A) { return T{}; } operator -(A,A)475 friend auto operator-(A, A) { return T{}; } 476 }; f(A<int> a)477 void f(A<int> a) { 478 int b = a(); 479 int c = a[0]; 480 int d = a + 0; 481 int e = +a; 482 int f = -a; 483 int g = a - a; 484 } 485 } 486 487 namespace TrailingReturnTypeForConversionOperator { 488 struct X { operator autoTrailingReturnTypeForConversionOperator::X489 operator auto() -> int { return 0; } // expected-error {{cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator'}} 490 } x; 491 int k = x.operator auto(); 492 493 struct Y { operator autoTrailingReturnTypeForConversionOperator::Y494 operator auto() -> int & { // expected-error {{cannot specify}} 495 return 0; // expected-error {{cannot bind to}} 496 } 497 }; 498 }; 499 500 namespace PR24989 { __anond37ef2410202(auto)501 auto x = [](auto){}; 502 using T = decltype(x); 503 void (T::*p)(int) const = &T::operator(); 504 } 505 forinit_decltypeauto()506 void forinit_decltypeauto() { 507 for (decltype(auto) forinit_decltypeauto_inner();;) {} // expected-warning {{interpreted as a function}} expected-note {{replace}} 508 } 509