Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class My_class {
- int x = 0;
- // ...
- void f() {
- int i = 0;
- // ...
- auto lambda = [=]{ use(i, x); }; // BAD: "looks like" copy/value capture
- // [&] has identical semantics and copies the this pointer under the current rules
- // [=,this] and [&,this] are not much better, and confusing
- x = 42;
- lambda(); // calls use(42);
- x = 43;
- lambda(); // calls use(43);
- // ...
- auto lambda2 = [i, this]{ use(i, x); }; // ok, most explicit and least confusing
- // ...
- }
- auto lambda = [=]{ use(i, x); };
- auto lambda2 = [i, this]{ use(i, x); };
- #include<iostream>
- using namespace std;
- class My_class {
- public:
- int x = 0;
- // ...
- void f() {
- int i = 0;
- // ...
- auto lambda = [=]{ cout<<i<<x<<endl; }; // BAD: "looks like" copy/value capture
- // [&] has identical semantics and copies the this pointer under the current rules
- // [=,this] and [&,this] are not much better, and confusing
- x = 42;
- lambda(); // calls use(42);
- x = 43;
- lambda(); // calls use(43);
- // ...
- auto lambda2 = [i, this]{ cout<<i<<x<<endl; }; // ok, most explicit and least confusing
- lambda2();
- }
- };
- int main()
- {
- My_class val;
- val.f();
- }
- 042
- 043
- 043
- Program ended with exit code: 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement