-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-capture-this-cpp20.cpp
More file actions
54 lines (45 loc) · 1.53 KB
/
lambda-capture-this-cpp20.cpp
File metadata and controls
54 lines (45 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wdeprecated-this-capture"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
// lambda capture this
// - In C++20, [=] no longer implicitly captures this — you must write
// [=, this] explicitly to capture both by value and the current object.
//
// Key notes:
namespace {
//// before
// C++17 and earlier — [=] silently captured this
struct Foo {
int x = 10;
void run() {
auto fn = [=]() { return x; }; // implicitly captured this
// warning: implicit capture of 'this' with a capture default of
// '=' is deprecated
fn(); // worked but was unclear
}
};
//// now
// C++20 — explicit, choose your intent
struct Foo_ {
int x = 10;
void run() {
auto fn1 = [=, this]() { return x; }; // capture this + locals by value
auto fn2 = [&, this]() { x = 20; }; // capture this + locals by ref
auto fn3 = [*this]() { return x; }; // copy the whole object
}
};
//// [*this] — safest for async/deferred calls
struct Worker {
int value = 42;
void schedule() {
// object may be destroyed before lambda runs!
auto bad = [this]() { return value; }; // ❌ dangling ptr
auto safe = [*this]() { return value; }; // ✓ owns its own copy
// threadPool.post(safe);
}
};
}