p12.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
  2. void odr_used() {
  3. int i = 17;
  4. [i]{}();
  5. }
  6. struct ReachingThis {
  7. static void static_foo() {
  8. (void)[this](){}; // expected-error{{'this' cannot be captured in this context}}
  9. struct Local {
  10. int i;
  11. void bar() {
  12. (void)[this](){};
  13. (void)[&](){i = 7; };
  14. }
  15. };
  16. }
  17. void foo() {
  18. (void)[this](){};
  19. struct Local {
  20. int i;
  21. static void static_bar() {
  22. (void)[this](){}; // expected-error{{'this' cannot be captured in this context}}
  23. (void)[&](){i = 7; }; // expected-error{{invalid use of member 'i' in static member function}}
  24. }
  25. };
  26. }
  27. };
  28. void immediately_enclosing(int i) { // expected-note{{'i' declared here}}
  29. [i]() {
  30. [i] {}();
  31. }();
  32. [=]() {
  33. [i] {}();
  34. }();
  35. []() { // expected-note{{lambda expression begins here}}
  36. [i] {}(); // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}
  37. }();
  38. }
  39. void f1(int i) { // expected-note{{declared here}}
  40. int const N = 20;
  41. auto m1 = [=]{
  42. int const M = 30;
  43. auto m2 = [i]{
  44. int x[N][M];
  45. x[0][0] = i;
  46. };
  47. (void)N;
  48. (void)M;
  49. (void)m2;
  50. };
  51. struct s1 {
  52. int f;
  53. void work(int n) { // expected-note{{declared here}}
  54. int m = n*n;
  55. int j = 40; // expected-note{{declared here}}
  56. auto m3 = [this,m] { // expected-note 3{{lambda expression begins here}}
  57. auto m4 = [&,j] { // expected-error{{variable 'j' cannot be implicitly captured in a lambda with no capture-default specified}}
  58. int x = n; // expected-error{{variable 'n' cannot be implicitly captured in a lambda with no capture-default specified}}
  59. x += m;
  60. x += i; // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}
  61. x += f;
  62. };
  63. };
  64. }
  65. };
  66. }