lambda-expressions.cpp 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify -fblocks %s
  2. namespace std { class type_info; };
  3. namespace ExplicitCapture {
  4. class C {
  5. int Member;
  6. static void Overload(int);
  7. void Overload();
  8. virtual C& Overload(float);
  9. void ImplicitThisCapture() {
  10. [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}} expected-error {{not supported yet}}
  11. [&](){(void)Member;}; // expected-error {{not supported yet}}
  12. // 'this' captures below don't actually work yet
  13. [this](){(void)Member;}; // expected-error{{lambda expressions are not supported yet}}
  14. [this]{[this]{};}; // expected-error 2{{lambda expressions are not supported yet}}
  15. []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}} expected-error 2 {{not supported yet}}
  16. []{Overload(3);}; // expected-error {{not supported yet}}
  17. []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}} expected-error {{not supported yet}}
  18. []{(void)typeid(Overload());};// expected-error {{not supported yet}}
  19. []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}} expected-error {{not supported yet}}
  20. }
  21. };
  22. void f() {
  23. [this] () {}; // expected-error {{'this' cannot be captured in this context}} expected-error {{not supported yet}}
  24. }
  25. }
  26. namespace ReturnDeduction {
  27. void test() {
  28. [](){ return 1; }; // expected-error {{not supported yet}}
  29. [](){ return 1; }; // expected-error {{not supported yet}}
  30. [](){ return ({return 1; 1;}); }; // expected-error {{not supported yet}}
  31. [](){ return ({return 'c'; 1;}); }; // expected-error {{not supported yet}} expected-error {{must match previous return type}}
  32. []()->int{ return 'c'; return 1; }; // expected-error {{not supported yet}}
  33. [](){ return 'c'; return 1; }; // expected-error {{not supported yet}} expected-error {{must match previous return type}}
  34. []() { return; return (void)0; }; // expected-error {{not supported yet}}
  35. // FIXME: Need to check structure of lambda body
  36. [](){ return 1; return 1; }; // expected-error {{not supported yet}}
  37. }
  38. }
  39. namespace ImplicitCapture {
  40. void test() {
  41. int a = 0; // expected-note 3 {{declared}}
  42. []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}} expected-error {{not supported yet}}
  43. [&]() { return a; }; // expected-error {{not supported yet}}
  44. [=]() { return a; }; // expected-error {{not supported yet}}
  45. [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}} expected-error {{not supported yet}}
  46. [=]() { return [&]() { return a; }; }; // expected-error 2 {{not supported yet}}
  47. []() { return [&]() { return a; }; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-error 2 {{not supported yet}}
  48. []() { return ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-error {{not supported yet}}
  49. const int b = 2;
  50. []() { return b; }; // expected-error {{not supported yet}}
  51. union { // expected-note {{declared}}
  52. int c;
  53. float d;
  54. };
  55. d = 3;
  56. [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} expected-error {{not supported yet}}
  57. __block int e; // expected-note {{declared}}
  58. [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}} expected-error {{not supported yet}}
  59. int f[10]; // expected-note {{declared}}
  60. [&]() { return f[2]; }; // expected-error {{not supported yet}}
  61. (void) ^{ return []() { return f[2]; }; }; // expected-error {{cannot refer to declaration with an array type inside block}} expected-error {{not supported yet}}
  62. struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
  63. G g;
  64. [=]() { const G* gg = &g; return gg->a; }; // expected-error {{not supported yet}}
  65. [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'const ImplicitCapture::G'}} expected-error 2 {{not supported yet}}
  66. (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const ImplicitCapture::G'}} expected-error {{not supported yet}}
  67. }
  68. }