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