78 lines · cpp
1// RUN: %clang_cc1 -std=c++11 %s -Wunused -Wno-unused-but-set-variable -Wno-unused-lambda-capture -verify2 3void odr_used() {4 int i = 17;5 [i]{}();6}7 8struct 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}}30 (void)[&](){i = 7; }; // expected-error{{invalid use of member 'i' in static member function}}31 }32 };33 }34};35 36void 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}} expected-note 2 {{capture 'i' by}} expected-note 2 {{default capture by}}46 [i] {}(); // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}47 }();48}49 50void f1(int i) { // expected-note{{declared here}}51 int const N = 20;52 auto m1 = [=]{53 int const M = 30;54 auto m2 = [i]{55 int x[N][M];56 x[0][0] = i;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}}67 auto m3 = [this, m] { // expected-note 3{{lambda expression begins here}} expected-note 2 {{capture 'i' by}} expected-note 2 {{capture 'j' by}} expected-note 2 {{capture 'n' by}}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;71 x += i; // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}72 x += f;73 };74 };75 } 76 };77}78