cxx1y-deduced-return-type.cpp revision f93ec7608aa79f9aac09304bfd3bc1918caf128f
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 4auto f(); // expected-note {{previous}} 5int f(); // expected-error {{differ only in their return type}} 6 7auto &g(); 8auto g() -> auto &; 9 10auto h() -> auto *; 11auto *h(); 12 13struct Conv1 { 14 operator auto(); // expected-note {{declared here}} 15} conv1; 16int 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}} 18Conv1::operator auto() { return 123; } 19int conv1b = conv1; 20int conv1c = conv1.operator auto(); 21int conv1d = conv1.operator int(); // expected-error {{no member named 'operator int'}} 22 23struct Conv2 { 24 operator auto() { return 0; } // expected-note 2{{previous}} 25 operator auto() { return 0.; } // expected-error {{cannot be redeclared}} expected-error {{redefinition of 'operator auto'}} 26}; 27 28struct Conv3 { 29 operator auto() { int *p = nullptr; return p; } // expected-note {{candidate}} 30 operator auto*() { int *p = nullptr; return p; } // expected-note {{candidate}} 31} conv3; 32int *conv3a = conv3; // expected-error {{ambiguous}} 33int *conv3b = conv3.operator auto(); 34int *conv3c = conv3.operator auto*(); 35 36template<typename T> 37struct Conv4 { 38 operator auto() { return T(); } 39}; 40Conv4<int> conv4int; 41int conv4a = conv4int; 42int conv4b = conv4int.operator auto(); 43 44auto a(); 45auto a() { return 0; } 46using T = decltype(a()); 47using T = int; 48auto a(); // expected-note {{previous}} 49using T = decltype(a()); 50auto *a(); // expected-error {{differ only in their return type}} 51 52auto b(bool k) { 53 if (k) 54 return "hello"; 55 return "goodbye"; 56} 57 58auto *ptr_1() { 59 return 100; // expected-error {{cannot deduce return type 'auto *' from returned value of type 'int'}} 60} 61 62const auto &ref_1() { 63 return 0; // expected-warning {{returning reference to local temporary}} 64} 65 66auto init_list() { 67 return { 1, 2, 3 }; // expected-error {{cannot deduce return type from initializer list}} 68} 69 70auto fwd_decl(); // expected-note 2{{here}} 71 72int n = fwd_decl(); // expected-error {{function 'fwd_decl' with deduced return type cannot be used before it is defined}} 73int k = sizeof(fwd_decl()); // expected-error {{used before it is defined}} 74 75auto fac(int n) { 76 if (n <= 2) 77 return n; 78 return n * fac(n-1); // ok 79} 80 81auto 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 87auto void_ret() {} 88using Void = void; 89using Void = decltype(void_ret()); 90 91auto &void_ret_2() {} // expected-error {{cannot deduce return type 'auto &' for function with no return statements}} 92const auto void_ret_3() {} // ok, return type 'const void' is adjusted to 'void' 93 94const 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 102namespace Templates { 103 template<typename T> auto f1() { 104 return T() + 1; 105 } 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}} 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 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}} 135 template<> auto instantiate<char>() {} // ok 136 template<> void instantiate<double>() {} // expected-error {{no function template matches}} 137 138 template<typename T> auto arg_single() { return 0; } 139 template<typename T> auto arg_multi() { return 0l; } 140 template<typename T> auto arg_multi(int) { return "bad"; } 141 template<typename T> struct Outer { 142 static auto arg_single() { return 0.f; } 143 static auto arg_multi() { return 0.; } 144 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 { 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}} 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 { 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}} 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 { 189 template<typename T> auto f(T) { return 0; } 190 template<typename T> void g(T(*)(int)); // expected-note {{couldn't infer}} 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 { 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 212auto fwd_decl_using(); 213namespace N { using ::fwd_decl_using; } 214auto fwd_decl_using() { return 0; } 215namespace N { int k = N::fwd_decl_using(); } 216 217namespace 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 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 231 auto f(int) { return 0.0; } 232 233 int &c = g(f); // ok 234} 235 236namespace 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 245 auto f() { return 0; } 246 247 int &b = g(f); // ok (presumably), due to deduction failure forming type of 'f<int>' 248 249 template<typename T> auto f(T) { return 0; } 250 251 int &c = g(f); // expected-error {{ambiguous}} 252} 253 254namespace 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 262namespace Constexpr { 263 constexpr auto f1(int n) { return n; } 264 struct NonLiteral { ~NonLiteral(); } nl; // expected-note {{user-provided destructor}} 265 constexpr auto f2(int n) { return nl; } // expected-error {{return type 'Constexpr::NonLiteral' is not a literal type}} 266} 267 268// It's not really clear whether these are valid, but this matches g++. 269using size_t = decltype(sizeof(0)); 270auto operator new(size_t n, const char*); // expected-error {{must return type 'void *'}} 271auto operator delete(void *, const char*); // expected-error {{must return type 'void'}} 272 273namespace Virtual { 274 struct S { 275 virtual auto f() { return 0; } // expected-error {{function with deduced return type cannot be virtual}} expected-note {{here}} 276 }; 277 // Allow 'auto' anyway for error recovery. 278 struct T : S { 279 int f(); 280 }; 281 struct U : S { 282 auto f(); // expected-error {{different return}} 283 }; 284 285 // And here's why... 286 struct V { virtual auto f(); }; // expected-error {{cannot be virtual}} 287 struct W : V { virtual auto f(); }; // expected-error {{cannot be virtual}} 288 auto V::f() { return 0; } // in tu1.cpp 289 auto W::f() { return 0.0; } // in tu2.cpp 290 W w; 291 int k1 = w.f(); 292 int k2 = ((V&)w).f(); 293} 294 295namespace std_examples { 296 297namespace NoReturn { 298 auto f() {} 299 void (*p)() = &f; 300 301 auto f(); // ok 302 303 auto *g() {} // expected-error {{cannot deduce return type 'auto *' for function with no return statements}} 304 305 auto h() = delete; // expected-note {{explicitly deleted}} 306 auto x = h(); // expected-error {{call to deleted}} 307} 308 309namespace UseBeforeComplete { 310 auto n = n; // expected-error {{variable 'n' declared with 'auto' type cannot appear in its own initializer}} 311 auto f(); // expected-note {{declared here}} 312 void g() { &f; } // expected-error {{function 'f' with deduced return type cannot be used before it is defined}} 313 auto sum(int i) { 314 if (i == 1) 315 return i; 316 else 317 return sum(i - 1) + i; 318 } 319} 320 321namespace Redecl { 322 auto f(); 323 auto f() { return 42; } 324 auto f(); // expected-note 2{{previous}} 325 int f(); // expected-error {{functions that differ only in their return type cannot be overloaded}} 326 decltype(auto) f(); // expected-error {{cannot be overloaded}} 327 328 template <typename T> auto g(T t) { return t; } // expected-note {{candidate}} \ 329 // expected-note {{candidate function [with T = int]}} 330 template auto g(int); 331 template char g(char); // expected-error {{does not refer to a function}} 332 template<> auto g(double); 333 334 template<typename T> T g(T t) { return t; } // expected-note {{candidate}} 335 template char g(char); 336 template auto g(float); 337 338 void h() { return g(42); } // expected-error {{ambiguous}} 339} 340 341namespace ExplicitInstantiationDecl { 342 template<typename T> auto f(T t) { return t; } 343 extern template auto f(int); 344 int (*p)(int) = f; 345} 346namespace MemberTemplatesWithDeduction { 347 struct M { 348 template<class T> auto foo(T t) { return t; } 349 template<class T> auto operator()(T t) const { return t; } 350 template<class T> static __attribute__((unused)) int static_foo(T) { 351 return 5; 352 } 353 template<class T> operator T() { return T{}; } 354 operator auto() { return &static_foo<int>; } 355 }; 356 struct N : M { 357 using M::foo; 358 using M::operator(); 359 using M::static_foo; 360 using M::operator auto; 361 }; 362 363 template <class T> int test() { 364 int i = T{}.foo(3); 365 T m = T{}.foo(M{}); 366 int j = T{}(3); 367 M m2 = M{}(M{}); 368 int k = T{}.static_foo(4); 369 int l = T::static_foo(5); 370 int l2 = T{}; 371 struct X { }; 372 X x = T{}; 373 return 0; 374 } 375 int Minst = test<M>(); 376 int Ninst = test<N>(); 377 378} 379} 380 381namespace CurrentInstantiation { 382 // PR16875 383 template<typename T> struct S { 384 auto f() { return T(); } 385 int g() { return f(); } 386 auto h(bool b) { 387 if (b) 388 return T(); 389 return h(true); 390 } 391 }; 392 int k1 = S<int>().g(); 393 int k2 = S<int>().h(false); 394 395 template<typename T> struct U { 396 #ifndef DELAYED_TEMPLATE_PARSING 397 auto f(); // expected-note {{here}} 398 int g() { return f(); } // expected-error {{cannot be used before it is defined}} 399 #else 400 auto f(); 401 int g() { return f(); } 402 #endif 403 }; 404 #ifndef DELAYED_TEMPLATE_PARSING 405 template int U<int>::g(); // expected-note {{in instantiation of}} 406 #else 407 template int U<int>::g(); 408 #endif 409 template<typename T> auto U<T>::f() { return T(); } 410 template int U<short>::g(); // ok 411} 412 413namespace WithDefaultArgs { 414 template<typename U> struct A { 415 template<typename T = U> friend auto f(A) { return []{}; } 416 }; 417 template<typename T> void f(); 418 using T = decltype(f(A<int>())); 419 using T = decltype(f<int>(A<int>())); 420} 421 422namespace MultilevelDeduction { 423 424auto F() -> auto* { return (int*)0; } 425 426auto (*G())() -> int* { return F; } 427 428auto run = G(); 429 430namespace Templated { 431template<class T> 432auto F(T t) -> auto* { return (T*)0; } 433 434template<class T> 435auto (*G(T t))(T) -> T* { return &F<T>; } 436 437 438template<class T> 439auto (*G2(T t))(T) -> auto* { return &F<T>; } 440 441auto run_int = G(1); 442auto run_char = G2('a'); 443 444} 445} 446 447namespace rnk { 448extern "C" int puts(const char *s); 449template <typename T> 450auto foo(T x) -> decltype(x) { 451#ifdef DELAYED_TEMPLATE_PARSING 452 ::rnk::bar(); 453#endif 454 return x; 455} 456void bar() { puts("bar"); } 457int main() { return foo(0); } 458 459} 460 461namespace OverloadedOperators { 462 template<typename T> struct A { 463 auto operator()() { return T{}; } 464 auto operator[](int) { return T{}; } 465 auto operator+(int) { return T{}; } 466 auto operator+() { return T{}; } 467 friend auto operator-(A) { return T{}; } 468 friend auto operator-(A, A) { return T{}; } 469 }; 470 void f(A<int> a) { 471 int b = a(); 472 int c = a[0]; 473 int d = a + 0; 474 int e = +a; 475 int f = -a; 476 int g = a - a; 477 } 478} 479