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
4 auto f(); // expected-note {{previous}}
5 int f(); // expected-error {{differ only in their return type}}
14 operator auto(); // expected-note {{declared here}}
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}}
18 Conv1::operator auto() { return 123; }
20 int conv1c = conv1.operator auto();
21 int conv1d = conv1.operator int(); // expected-error {{no member named 'operator int'}}
24 operator auto() { return 0; } // expected-note {{previous}}
25 operator auto() { return 0.; } // expected-error {{cannot be redeclared}} expected-error {{cannot initialize return object of type 'auto' with an rvalue of type 'double'}}
29 operator auto() { int *p = nullptr; return p; } // expected-note {{candidate}}
30 operator auto*() { int *p = nullptr; return p; } // expected-note {{candidate}}
32 int *conv3a = conv3; // expected-error {{ambiguous}}
33 int *conv3b = conv3.operator auto();
34 int *conv3c = conv3.operator auto*();
38 operator auto() { return T(); }
41 int conv4a = conv4int;
42 int conv4b = conv4int.operator auto();
45 auto a() { return 0; }
46 using T = decltype(a());
48 auto a(); // expected-note {{previous}}
49 using T = decltype(a());
50 auto *a(); // expected-error {{differ only in their return type}}
59 return 100; // expected-error {{cannot deduce return type 'auto *' from returned value of type 'int'}}
63 return 0; // expected-warning {{returning reference to local temporary}}
67 return { 1, 2, 3 }; // expected-error {{cannot deduce return type from initializer list}}
70 auto fwd_decl(); // expected-note 2{{here}}
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}}
78 return n * fac(n-1); // ok
81 auto fac_2(int n) { // expected-note {{declared here}}
83 return n * fac_2(n-1); // expected-error {{cannot be used before it is defined}}
89 using Void = decltype(void_ret());
91 auto &void_ret_2() {} // expected-error {{cannot deduce return type 'auto &' for function with no return statements}}
92 const auto void_ret_3() {} // ok, return type 'const void' is adjusted to 'void'
94 const auto void_ret_4() {
99 return 0; // expected-error {{'auto' in return type deduced as 'int' here but deduced as 'void' in earlier return statement}}
102 namespace Templates {
103 template<typename T> auto f1() {
106 template<typename T> auto &f2(T &&v) { return v; }
108 const int &b = f2(0);
110 float &c = f2(0.0); // expected-error {{non-const lvalue reference to type 'float' cannot bind to a value of unrelated type 'double'}}
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>();
120 auto (*p)() = f1; // expected-error {{incompatible initializer}}
121 auto (*q)() = f1<int>; // ok
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 &'))}}
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>();
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}}
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"; }
146 template<typename T> T &take_fn(T (*p)());
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>);
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);
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}}
167 auto (*q)() = f<int>;
168 int (*r)() = f; // expected-error {{does not match}}
170 g<int>(f); // expected-error {{no matching function}}
171 g(f); // expected-error {{no matching function}}
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}}
180 auto (*q)(int) = f<int>;
181 int (*r)(int) = f; // expected-error {{does not match}}
183 g<int>(f); // expected-error {{no matching function}}
184 g(f); // expected-error {{no matching function}}
189 template<typename T> auto f(T) { return 0; }
190 template<typename T> void g(T(*)(int)); // expected-note {{couldn't infer}}
193 auto (*q)(int) = f<int>;
194 int (*r)(int) = f; // ok
197 g(f); // expected-error {{no matching function}}
201 namespace DeduceInDeducedReturnType {
202 template<typename T, typename U> auto f() -> auto (T::*)(U) {
203 int (T::*result)(U) = nullptr;
207 int (S::*(*p)())(double) = f;
208 int (S::*(*q)())(double) = f<S, double>;
212 auto fwd_decl_using();
213 namespace N { using ::fwd_decl_using; }
214 auto fwd_decl_using() { return 0; }
215 namespace N { int k = N::fwd_decl_using(); }
217 namespace OverloadResolutionNonTemplate {
219 auto f(int); // expected-note {{here}}
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)'}}
224 int a = g(f); // expected-error {{no matching function}}
226 auto f() { return 0; }
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}}
231 auto f(int) { return 0.0; }
236 namespace OverloadResolutionTemplate {
238 template<typename T> auto f(T);
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}}
243 int a = g(f); // expected-error {{no matching function}}
245 auto f() { return 0; }
247 int &b = g(f); // ok (presumably), due to deduction failure forming type of 'f<int>'
249 template<typename T> auto f(T) { return 0; }
251 int &c = g(f); // expected-error {{ambiguous}}
254 namespace DefaultedMethods {
256 auto operator=(const A&) = default; // expected-error {{must return 'DefaultedMethods::A &'}}
257 A &operator=(A&&); // expected-note {{previous}}
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}}
262 namespace Constexpr {
263 constexpr auto f1(int n) { return n; }
264 template<typename T> struct X { constexpr auto f() {} }; // PR18746
265 template<typename T> struct Y { constexpr T f() {} }; // expected-note {{control reached end of constexpr function}}
269 constexpr int q = Y<int>().f(); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to '&Y<int>()->f()'}}
271 struct NonLiteral { ~NonLiteral(); } nl; // expected-note {{user-provided destructor}}
272 constexpr auto f2(int n) { return nl; } // expected-error {{return type 'Constexpr::NonLiteral' is not a literal type}}
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'}}
282 virtual auto f() { return 0; } // expected-error {{function with deduced return type cannot be virtual}} expected-note {{here}}
284 // Allow 'auto' anyway for error recovery.
289 auto f(); // expected-error {{different return}}
293 struct V { virtual auto f(); }; // expected-error {{cannot be virtual}}
294 struct W : V { virtual auto f(); }; // expected-error {{cannot be virtual}}
295 auto V::f() { return 0; } // in tu1.cpp
296 auto W::f() { return 0.0; } // in tu2.cpp
299 int k2 = ((V&)w).f();
302 namespace std_examples {
310 auto *g() {} // expected-error {{cannot deduce return type 'auto *' for function with no return statements}}
312 auto h() = delete; // expected-note {{explicitly deleted}}
313 auto x = h(); // expected-error {{call to deleted}}
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}}
319 void g() { &f; } // expected-error {{function 'f' with deduced return type cannot be used before it is defined}}
324 return sum(i - 1) + i;
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}}
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);
341 template<typename T> T g(T t) { return t; } // expected-note {{candidate}}
342 template char g(char);
343 template auto g(float);
345 void h() { return g(42); } // expected-error {{ambiguous}}
348 namespace ExplicitInstantiationDecl {
349 template<typename T> auto f(T t) { return t; }
350 extern template auto f(int);
353 namespace MemberTemplatesWithDeduction {
355 template<class T> auto foo(T t) { return t; }
356 template<class T> auto operator()(T t) const { return t; }
357 template<class T> static __attribute__((unused)) int static_foo(T) {
360 template<class T> operator T() { return T{}; }
361 operator auto() { return &static_foo<int>; }
367 using M::operator auto;
370 template <class T> int test() {
375 int k = T{}.static_foo(4);
376 int l = T::static_foo(5);
382 int Minst = test<M>();
383 int Ninst = test<N>();
388 namespace CurrentInstantiation {
390 template<typename T> struct S {
391 auto f() { return T(); }
392 int g() { return f(); }
399 int k1 = S<int>().g();
400 int k2 = S<int>().h(false);
402 template<typename T> struct U {
403 #ifndef DELAYED_TEMPLATE_PARSING
404 auto f(); // expected-note {{here}}
405 int g() { return f(); } // expected-error {{cannot be used before it is defined}}
408 int g() { return f(); }
411 #ifndef DELAYED_TEMPLATE_PARSING
412 template int U<int>::g(); // expected-note {{in instantiation of}}
414 template int U<int>::g();
416 template<typename T> auto U<T>::f() { return T(); }
417 template int U<short>::g(); // ok
420 namespace WithDefaultArgs {
421 template<typename U> struct A {
422 template<typename T = U> friend auto f(A) { return []{}; }
424 template<typename T> void f();
425 using T = decltype(f(A<int>()));
426 using T = decltype(f<int>(A<int>()));
429 namespace MultilevelDeduction {
431 auto F() -> auto* { return (int*)0; }
433 auto (*G())() -> int* { return F; }
437 namespace Templated {
439 auto F(T t) -> auto* { return (T*)0; }
442 auto (*G(T t))(T) -> T* { return &F<T>; }
446 auto (*G2(T t))(T) -> auto* { return &F<T>; }
449 auto run_char = G2('a');
455 extern "C" int puts(const char *s);
456 template <typename T>
457 auto foo(T x) -> decltype(x) {
458 #ifdef DELAYED_TEMPLATE_PARSING
463 void bar() { puts("bar"); }
464 int main() { return foo(0); }
468 namespace OverloadedOperators {
469 template<typename T> struct A {
470 auto operator()() { return T{}; }
471 auto operator[](int) { return T{}; }
472 auto operator+(int) { return T{}; }
473 auto operator+() { return T{}; }
474 friend auto operator-(A) { return T{}; }
475 friend auto operator-(A, A) { return T{}; }
487 namespace TrailingReturnTypeForConversionOperator {
489 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'}}
491 int k = x.operator auto();
494 operator auto() -> int & { // expected-error {{cannot specify}}
495 return 0; // expected-error {{cannot bind to}}