cxx1y-deduced-return-type.cpp revision 60e141e1f87211ca831de6821003d80fe20a06f3
1// RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s
2
3auto f(); // expected-note {{previous}}
4int f(); // expected-error {{differ only in their return type}}
5
6auto &g();
7auto g() -> auto &;
8
9auto h() -> auto *;
10auto *h();
11
12struct Conv1 {
13  operator auto(); // expected-note {{declared here}}
14} conv1;
15int conv1a = conv1; // expected-error {{function 'operator auto' with deduced return type cannot be used before it is defined}}
16// expected-error@-1 {{no viable conversion}}
17Conv1::operator auto() { return 123; }
18int conv1b = conv1;
19int conv1c = conv1.operator auto();
20int conv1d = conv1.operator int(); // expected-error {{no member named 'operator int'}}
21
22struct Conv2 {
23  operator auto() { return 0; }  // expected-note 2{{previous}}
24  operator auto() { return 0.; } // expected-error {{cannot be redeclared}} expected-error {{redefinition of 'operator auto'}}
25};
26
27struct Conv3 {
28  operator auto() { int *p = nullptr; return p; }  // expected-note {{candidate}}
29  operator auto*() { int *p = nullptr; return p; } // expected-note {{candidate}}
30} conv3;
31int *conv3a = conv3; // expected-error {{ambiguous}}
32int *conv3b = conv3.operator auto();
33int *conv3c = conv3.operator auto*();
34
35template<typename T>
36struct Conv4 {
37  operator auto() { return T(); }
38};
39Conv4<int> conv4int;
40int conv4a = conv4int;
41int conv4b = conv4int.operator auto();
42
43auto a();
44auto a() { return 0; }
45using T = decltype(a());
46using T = int;
47auto a(); // expected-note {{previous}}
48using T = decltype(a());
49auto *a(); // expected-error {{differ only in their return type}}
50
51auto b(bool k) {
52  if (k)
53    return "hello";
54  return "goodbye";
55}
56
57auto *ptr_1() {
58  return 100; // expected-error {{cannot deduce return type 'auto *' from returned value of type 'int'}}
59}
60
61const auto &ref_1() {
62  return 0; // expected-warning {{returning reference to local temporary}}
63}
64
65auto init_list() {
66  return { 1, 2, 3 }; // expected-error {{cannot deduce return type from initializer list}}
67}
68
69auto fwd_decl(); // expected-note 2{{here}}
70
71int n = fwd_decl(); // expected-error {{function 'fwd_decl' with deduced return type cannot be used before it is defined}}
72int k = sizeof(fwd_decl()); // expected-error {{used before it is defined}}
73
74auto fac(int n) {
75  if (n <= 2)
76    return n;
77  return n * fac(n-1); // ok
78}
79
80auto fac_2(int n) { // expected-note {{declared here}}
81  if (n > 2)
82    return n * fac_2(n-1); // expected-error {{cannot be used before it is defined}}
83  return n;
84}
85
86auto void_ret() {}
87using Void = void;
88using Void = decltype(void_ret());
89
90auto &void_ret_2() {} // expected-error {{cannot deduce return type 'auto &' for function with no return statements}}
91const auto void_ret_3() {} // ok, return type 'const void' is adjusted to 'void'
92
93const auto void_ret_4() {
94  if (false)
95    return void();
96  if (false)
97    return;
98  return 0; // expected-error {{'auto' in return type deduced as 'int' here but deduced as 'void' in earlier return statement}}
99}
100
101namespace Templates {
102  template<typename T> auto f1() {
103    return T() + 1;
104  }
105  template<typename T> auto &f2(T &&v) { return v; }
106  int a = f1<int>();
107  const int &b = f2(0);
108  double d;
109  float &c = f2(0.0); // expected-error {{non-const lvalue reference to type 'float' cannot bind to a value of unrelated type 'double'}}
110
111  template<typename T> auto fwd_decl(); // expected-note {{declared here}}
112  int e = fwd_decl<int>(); // expected-error {{cannot be used before it is defined}}
113  template<typename T> auto fwd_decl() { return 0; }
114  int f = fwd_decl<int>();
115  template<typename T> auto fwd_decl();
116  int g = fwd_decl<char>();
117
118  auto (*p)() = f1; // expected-error {{incompatible initializer}}
119  auto (*q)() = f1<int>; // ok
120
121  typedef decltype(f2(1.2)) dbl; // expected-note {{previous}}
122  typedef float dbl; // expected-error {{typedef redefinition with different types ('float' vs 'decltype(f2(1.2))' (aka 'double &'))}}
123
124  extern template auto fwd_decl<double>();
125  int k1 = fwd_decl<double>();
126  extern template int fwd_decl<char>(); // expected-error {{does not refer to a function template}}
127  int k2 = fwd_decl<char>();
128
129  template<typename T> auto instantiate() { T::error; } // expected-error {{has no members}}
130  extern template auto instantiate<int>(); // ok
131  int k = instantiate<int>(); // expected-note {{in instantiation of}}
132  template<> auto instantiate<char>() {} // ok
133  template<> void instantiate<double>() {} // expected-error {{no function template matches}}
134
135  template<typename T> auto arg_single() { return 0; }
136  template<typename T> auto arg_multi() { return 0l; }
137  template<typename T> auto arg_multi(int) { return "bad"; }
138  template<typename T> struct Outer {
139    static auto arg_single() { return 0.f; }
140    static auto arg_multi() { return 0.; }
141    static auto arg_multi(int) { return "bad"; }
142  };
143  template<typename T> T &take_fn(T (*p)());
144
145  int &check1 = take_fn(arg_single); // expected-error {{no matching}} expected-note@-2 {{couldn't infer}}
146  int &check2 = take_fn(arg_single<int>);
147  int &check3 = take_fn<int>(arg_single); // expected-error {{no matching}} expected-note@-4{{no overload of 'arg_single'}}
148  int &check4 = take_fn<int>(arg_single<int>);
149  long &check5 = take_fn(arg_multi); // expected-error {{no matching}} expected-note@-6 {{couldn't infer}}
150  long &check6 = take_fn(arg_multi<int>);
151  long &check7 = take_fn<long>(arg_multi); // expected-error {{no matching}} expected-note@-8{{no overload of 'arg_multi'}}
152  long &check8 = take_fn<long>(arg_multi<int>);
153
154  float &mem_check1 = take_fn(Outer<int>::arg_single);
155  float &mem_check2 = take_fn<float>(Outer<char>::arg_single);
156  double &mem_check3 = take_fn(Outer<long>::arg_multi);
157  double &mem_check4 = take_fn<double>(Outer<double>::arg_multi);
158
159  namespace Deduce1 {
160    template<typename T> auto f() { return 0; } // expected-note {{candidate}}
161    template<typename T> void g(T(*)()); // expected-note 2{{candidate}}
162    void h() {
163      auto p = f<int>;
164      auto (*q)() = f<int>;
165      int (*r)() = f; // expected-error {{does not match}}
166      g(f<int>);
167      g<int>(f); // expected-error {{no matching function}}
168      g(f); // expected-error {{no matching function}}
169    }
170  }
171
172  namespace Deduce2 {
173    template<typename T> auto f(int) { return 0; } // expected-note {{candidate}}
174    template<typename T> void g(T(*)(int)); // expected-note 2{{candidate}}
175    void h() {
176      auto p = f<int>;
177      auto (*q)(int) = f<int>;
178      int (*r)(int) = f; // expected-error {{does not match}}
179      g(f<int>);
180      g<int>(f); // expected-error {{no matching function}}
181      g(f); // expected-error {{no matching function}}
182    }
183  }
184
185  namespace Deduce3 {
186    template<typename T> auto f(T) { return 0; }
187    template<typename T> void g(T(*)(int)); // expected-note {{couldn't infer}}
188    void h() {
189      auto p = f<int>;
190      auto (*q)(int) = f<int>;
191      int (*r)(int) = f; // ok
192      g(f<int>);
193      g<int>(f); // ok
194      g(f); // expected-error {{no matching function}}
195    }
196  }
197
198  namespace DeduceInDeducedReturnType {
199    template<typename T, typename U> auto f() -> auto (T::*)(U) {
200      int (T::*result)(U) = nullptr;
201      return result;
202    }
203    struct S {};
204    int (S::*(*p)())(double) = f;
205    int (S::*(*q)())(double) = f<S, double>;
206  }
207}
208
209auto fwd_decl_using();
210namespace N { using ::fwd_decl_using; }
211auto fwd_decl_using() { return 0; }
212namespace N { int k = N::fwd_decl_using(); }
213
214namespace OverloadResolutionNonTemplate {
215  auto f();
216  auto f(int); // expected-note {{here}}
217
218  int &g(int (*f)()); // expected-note {{not viable: no overload of 'f' matching 'int (*)()'}}
219  char &g(int (*f)(int)); // expected-note {{not viable: no overload of 'f' matching 'int (*)(int)'}}
220
221  int a = g(f); // expected-error {{no matching function}}
222
223  auto f() { return 0; }
224
225  // FIXME: It's not completely clear whether this should be ill-formed.
226  int &b = g(f); // expected-error {{used before it is defined}}
227
228  auto f(int) { return 0.0; }
229
230  int &c = g(f); // ok
231}
232
233namespace OverloadResolutionTemplate {
234  auto f();
235  template<typename T> auto f(T);
236
237  int &g(int (*f)()); // expected-note {{not viable: no overload of 'f' matching 'int (*)()'}} expected-note {{candidate}}
238  char &g(int (*f)(int)); // expected-note {{not viable: no overload of 'f' matching 'int (*)(int)'}} expected-note {{candidate}}
239
240  int a = g(f); // expected-error {{no matching function}}
241
242  auto f() { return 0; }
243
244  int &b = g(f); // ok (presumably), due to deduction failure forming type of 'f<int>'
245
246  template<typename T> auto f(T) { return 0; }
247
248  int &c = g(f); // expected-error {{ambiguous}}
249}
250
251namespace DefaultedMethods {
252  struct A {
253    auto operator=(const A&) = default; // expected-error {{must return 'DefaultedMethods::A &'}}
254    A &operator=(A&&); // expected-note {{previous}}
255  };
256  auto A::operator=(A&&) = default; // expected-error {{differs from the declaration in the return type}}
257}
258
259namespace Constexpr {
260  constexpr auto f1(int n) { return n; }
261  struct NonLiteral { ~NonLiteral(); } nl; // expected-note {{user-provided destructor}}
262  constexpr auto f2(int n) { return nl; } // expected-error {{return type 'Constexpr::NonLiteral' is not a literal type}}
263}
264
265// It's not really clear whether these are valid, but this matches g++.
266using size_t = decltype(sizeof(0));
267auto operator new(size_t n, const char*); // expected-error {{must return type 'void *'}}
268auto operator delete(void *, const char*); // expected-error {{must return type 'void'}}
269
270namespace Virtual {
271  struct S {
272    virtual auto f() { return 0; } // expected-error {{function with deduced return type cannot be virtual}} expected-note {{here}}
273  };
274  // Allow 'auto' anyway for error recovery.
275  struct T : S {
276    int f();
277  };
278  struct U : S {
279    auto f(); // expected-error {{different return}}
280  };
281
282  // And here's why...
283  struct V { virtual auto f(); }; // expected-error {{cannot be virtual}}
284  struct W : V { virtual auto f(); }; // expected-error {{cannot be virtual}}
285  auto V::f() { return 0; } // in tu1.cpp
286  auto W::f() { return 0.0; } // in tu2.cpp
287  W w;
288  int k1 = w.f();
289  int k2 = ((V&)w).f();
290}
291
292namespace std_examples {
293
294namespace NoReturn {
295  auto f() {}
296  void (*p)() = &f;
297
298  auto *g() {} // expected-error {{cannot deduce return type 'auto *' for function with no return statements}}
299}
300
301namespace UseBeforeComplete {
302  auto n = n; // expected-error {{variable 'n' declared with 'auto' type cannot appear in its own initializer}}
303  auto f(); // expected-note {{declared here}}
304  void g() { &f; } // expected-error {{function 'f' with deduced return type cannot be used before it is defined}}
305  auto sum(int i) {
306    if (i == 1)
307      return i;
308    else
309      return sum(i - 1) + i;
310  }
311}
312
313namespace Redecl {
314  auto f();
315  auto f() { return 42; }
316  auto f(); // expected-note 2{{previous}}
317  int f(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
318  decltype(auto) f(); // expected-error {{cannot be overloaded}}
319
320  template<typename T> auto g(T t) { return t; } // expected-note {{candidate}}
321  template auto g(int);
322  template char g(char); // expected-error {{does not refer to a function}}
323  template<> auto g(double);
324
325  template<typename T> T g(T t) { return t; } // expected-note {{candidate}}
326  template char g(char);
327  template auto g(float);
328
329  void h() { return g(42); } // expected-error {{ambiguous}}
330}
331
332namespace ExplicitInstantiationDecl {
333  template<typename T> auto f(T t) { return t; }
334  extern template auto f(int);
335  int (*p)(int) = f;
336}
337
338}
339