cxx0x-lambda-expressions.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
  2. enum E { e };
  3. constexpr int id(int n) { return n; }
  4. class C {
  5. int f() {
  6. int foo, bar;
  7. []; // expected-error {{expected body of lambda expression}}
  8. [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
  9. [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
  10. [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
  11. [&this] {}; // expected-error {{'this' cannot be captured by reference}}
  12. [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
  13. [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
  14. [] {};
  15. [=] (int i) {};
  16. [&] (int) mutable -> void {};
  17. [foo,bar] () { return 3; };
  18. [=,&foo] () {};
  19. [&,foo] () {};
  20. [this] () {};
  21. [] () -> class C { return C(); };
  22. [] () -> enum E { return e; };
  23. [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
  24. [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
  25. [](int) -> {}; // PR13652 expected-error {{expected a type}}
  26. return 1;
  27. }
  28. void designator_or_lambda() {
  29. typedef int T;
  30. const int b = 0;
  31. const int c = 1;
  32. int d;
  33. int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
  34. int a2[1] = {[b] = 1 };
  35. int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
  36. int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
  37. int a5[3] = { []{return 0;}() };
  38. int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
  39. int a7[1] = {[d(0)] { return d; } ()}; // expected-warning{{extension}}
  40. int a8[1] = {[d = 0] { return d; } ()}; // expected-warning{{extension}}
  41. int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
  42. int a10[1] = {[id(0)] { return id; } ()}; // expected-warning{{extension}}
  43. int a11[1] = {[id(0)] = 1};
  44. }
  45. void delete_lambda(int *p) {
  46. delete [] p;
  47. delete [] (int*) { new int }; // ok, compound-literal, not lambda
  48. delete [] { return new int; } (); // expected-error{{expected expression}}
  49. delete [&] { return new int; } (); // ok, lambda
  50. }
  51. // We support init-captures in C++11 as an extension.
  52. int z;
  53. void init_capture() {
  54. [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
  55. [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}} expected-warning{{will change meaning in a future version}}
  56. [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
  57. [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
  58. [a([&b = z]{})](){}; // expected-warning 2{{extension}}
  59. int x = 4;
  60. auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
  61. r += 2;
  62. return x + 2;
  63. } ();
  64. }
  65. void attributes() {
  66. [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
  67. [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
  68. []() [[]]
  69. mutable {}; // expected-error {{expected body of lambda expression}}
  70. []() [[]] {};
  71. []() [[]] -> void {};
  72. []() mutable [[]] -> void {};
  73. []() mutable noexcept [[]] -> void {};
  74. // Testing GNU-style attributes on lambdas -- the attribute is specified
  75. // before the mutable specifier instead of after (unlike C++11).
  76. []() __attribute__((noreturn)) mutable { while(1); };
  77. []() mutable
  78. __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
  79. }
  80. };
  81. template <typename>
  82. void PR22122() {
  83. [](int) -> {}; // expected-error {{expected a type}}
  84. }
  85. template void PR22122<int>();
  86. struct S {
  87. template <typename T>
  88. void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
  89. } s;
  90. struct U {
  91. template <typename T>
  92. void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
  93. } *U;