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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include<iostream> class Demo { public: Demo(int i):m(i){} void operator()() { [this] {std::cout << m << std::endl; }(); } void demo(int a) { [=,this] { this->m = a; }(); } void test() { [this] {this->m = 111; }(); } private: int m = 0; }; int main() {
std::cout << [] { return 5; }() << std::endl;
std::cout << []()->decltype(auto) {return 5; }() << std::endl;
std::cout << []()->int {return 5; }() << std::endl;
std::cout << []()->auto {return 5; }() << std::endl;
int a = 0; auto p = [](int b) {std::cout << b << std::endl; }; p(a); auto p2 = [](auto b) {std::cout << b << std::endl; }; p2(a);
[=] {std::cout << a << std::endl; }(); [i = a] {std::cout << i << std::endl; }();
[&] {a = 100; std::cout << a << std::endl; }();
[&a] {a = 100; std::cout << a << std::endl; }();
[ca=std::as_const(a)] ()mutable {ca = 10; std::cout << ca << std::endl; }(); std::cout << a << std::endl;
Demo demo(10);
demo();
demo.demo(123);
demo();
demo.test();
demo();
int e(0), f(0), g(0); [E = e, F = f, G = g] {std::cout << E << ' ' << F << ' ' << G << std::endl; }();
[&E = e, &F = f, &G = g] {E = 1, F = 2, G = 3; std::cout << E << ' ' << F << ' ' << G << std::endl; }(); std::cout << e << ' ' << f << ' ' << g << std::endl; return 0; }
|