1 | #include <cassert> |
---|---|
2 | #include <cstdio> |
3 | |
4 | namespace { |
5 | int global_var = -5; |
6 | } // namespace |
7 | |
8 | struct Baz { |
9 | virtual ~Baz() = default; |
10 | |
11 | virtual int baz_virt() = 0; |
12 | |
13 | int base_base_var = 12; |
14 | }; |
15 | |
16 | struct Bar : public Baz { |
17 | virtual ~Bar() = default; |
18 | |
19 | virtual int baz_virt() override { |
20 | base_var = 10; |
21 | return 1; |
22 | } |
23 | |
24 | int base_var = 15; |
25 | }; |
26 | |
27 | struct Foo : public Bar { |
28 | int class_var = 9; |
29 | int shadowed = -137; |
30 | int *class_ptr; |
31 | |
32 | virtual ~Foo() = default; |
33 | |
34 | virtual int baz_virt() override { |
35 | shadowed = -1; |
36 | return 2; |
37 | } |
38 | |
39 | void method() { |
40 | int local_var = 137; |
41 | int shadowed; |
42 | class_ptr = &local_var; |
43 | auto lambda = [&shadowed, this, &local_var, |
44 | local_var_copy = local_var]() mutable { |
45 | int lambda_local_var = 5; |
46 | shadowed = 5; |
47 | class_var = 109; |
48 | --base_var; |
49 | --base_base_var; |
50 | std::puts(s: "break here"); |
51 | |
52 | auto nested_lambda = [this, &lambda_local_var, local_var] { |
53 | std::puts(s: "break here"); |
54 | lambda_local_var = 0; |
55 | }; |
56 | |
57 | nested_lambda(); |
58 | --local_var_copy; |
59 | std::puts(s: "break here"); |
60 | |
61 | struct LocalLambdaClass { |
62 | int lambda_class_local = -12345; |
63 | Foo *outer_ptr; |
64 | |
65 | void inner_method() { |
66 | auto lambda = [this] { |
67 | std::puts(s: "break here"); |
68 | lambda_class_local = -2; |
69 | outer_ptr->class_var *= 2; |
70 | }; |
71 | |
72 | lambda(); |
73 | } |
74 | }; |
75 | |
76 | LocalLambdaClass l; |
77 | l.outer_ptr = this; |
78 | l.inner_method(); |
79 | }; |
80 | lambda(); |
81 | } |
82 | |
83 | void non_capturing_method() { |
84 | int local = 5; |
85 | int local2 = 10; |
86 | |
87 | class_var += [=] { |
88 | std::puts(s: "break here"); |
89 | return local + local2; |
90 | }(); |
91 | } |
92 | }; |
93 | |
94 | int main() { |
95 | Foo f; |
96 | f.method(); |
97 | f.non_capturing_method(); |
98 | return global_var; |
99 | } |
100 |