2
0

p4-1y.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++1y %s -verify
  2. int a;
  3. int &b = [] (int &r) -> decltype(auto) { return r; } (a);
  4. int &c = [] (int &r) -> decltype(auto) { return (r); } (a);
  5. int &d = [] (int &r) -> auto & { return r; } (a);
  6. int &e = [] (int &r) -> auto { return r; } (a); // expected-error {{cannot bind to a temporary}}
  7. int &f = [] (int r) -> decltype(auto) { return r; } (a); // expected-error {{cannot bind to a temporary}}
  8. int &g = [] (int r) -> decltype(auto) { return (r); } (a); // expected-warning {{reference to stack}}
  9. int test_explicit_auto_return()
  10. {
  11. struct X {};
  12. auto L = [](auto F, auto a) { return F(a); };
  13. auto M = [](auto a) -> auto { return a; }; // OK
  14. auto MRef = [](auto b) -> auto& { return b; }; //expected-warning{{reference to stack}}
  15. auto MPtr = [](auto c) -> auto* { return &c; }; //expected-warning{{address of stack}}
  16. auto MDeclType = [](auto&& d) -> decltype(auto) { return static_cast<decltype(d)>(d); }; //OK
  17. M(3);
  18. auto &&x = MDeclType(X{});
  19. auto &&x1 = M(X{});
  20. auto &&x2 = MRef(X{});//expected-note{{in instantiation of}}
  21. auto &&x3 = MPtr(X{}); //expected-note{{in instantiation of}}
  22. return 0;
  23. }
  24. int test_implicit_auto_return()
  25. {
  26. {
  27. auto M = [](auto a) { return a; };
  28. struct X {};
  29. X x = M(X{});
  30. }
  31. }
  32. int test_multiple_returns() {
  33. auto M = [](auto a) {
  34. bool k;
  35. if (k)
  36. return a;
  37. else
  38. return 5; //expected-error{{deduced as 'int' here}}
  39. };
  40. M(3); // OK
  41. M('a'); //expected-note{{in instantiation of}}
  42. return 0;
  43. }
  44. int test_no_parameter_list()
  45. {
  46. static int si = 0;
  47. auto M = [] { return 5; }; // OK
  48. auto M2 = [] -> auto&& { return si; }; // expected-error{{lambda requires '()'}}
  49. M();
  50. }
  51. int test_conditional_in_return() {
  52. auto Fac = [](auto f, auto n) {
  53. return n <= 0 ? n : f(f, n - 1) * n;
  54. };
  55. // FIXME: this test causes a recursive limit - need to error more gracefully.
  56. //Fac(Fac, 3);
  57. }