1 // P0018R3 - C++17 lambda capture of *this
2 // { dg-do run { target c++11 } }
3 // { dg-options "" }
4 
5 extern "C" void abort ();
6 
7 struct A {
8   int a, z;
AA9   A () : a (4), z (0) {}
AA10   A (const A &x) : a (x.a), z (1) {}
fooA11   void foo () {
12     if (z != 0) abort ();
13     auto b = [this] { return &a; };
14     auto c = [*this] { return &a; };	// { dg-warning "'*this' capture only available with" "" { target c++14_down } }
15     auto d = [=] { return &a; };
16     auto e = [&] { return &a; };
17     if (b () != &a) abort ();
18     if (*b () != 4) abort ();
19     auto f = c ();
20     if (c () == &a) abort ();
21     if (c () != f) abort ();
22     if (*c () != 4) abort ();
23     if (d () != &a) abort ();
24     if (e () != &a) abort ();
25     auto g = [this] { return a + z; };
26     auto h = [*this] { return a + z; };	// { dg-warning "'*this' capture only available with" "" { target c++14_down } }
27     auto i = [=] { return a + z; };
28     auto j = [&] { return a + z; };
29     if (g () != 4 || h () != 5 || i () != 4 || j () != 4) abort ();
30   }
31 };
32 
33 template <int N>
34 struct B {
35   int a, z;
BB36   B () : a (N), z (0) {}
BB37   B (const B &x) : a (x.a), z (1) {}
fooB38   void foo () {
39     if (z != 0) abort ();
40     auto b = [this] { return &a; };
41     auto c = [*this] { return &a; };	// { dg-warning "'*this' capture only available with" "" { target c++14_down } }
42     auto d = [=] { return &a; };
43     auto e = [&] { return &a; };
44     if (b () != &a) abort ();
45     if (*b () != 9) abort ();
46     auto f = c ();
47     if (c () == &a) abort ();
48     if (c () != f) abort ();
49     if (*c () != 9) abort ();
50     if (d () != &a) abort ();
51     if (e () != &a) abort ();
52     auto g = [this] { return a + z; };
53     auto h = [*this] { return a + z; };	// { dg-warning "'*this' capture only available with" "" { target c++14_down } }
54     auto i = [=] { return a + z; };
55     auto j = [&] { return a + z; };
56     if (g () != 9 || h () != 10 || i () != 9 || j () != 9) abort ();
57   }
58 };
59 
60 int
main()61 main ()
62 {
63   A a;
64   a.foo ();
65   B<9> b;
66   b.foo ();
67   return 0;
68 }
69