c++: P0847R7 (deducing this) - xobj lambdas. [PR102609]
[official-gcc.git] / gcc / testsuite / g++.dg / cpp23 / explicit-obj-lambda8.C
bloba068941413a3af58c033e73fa706af88c140d458
1 // P0847R7
2 // { dg-do run { target c++23 } }
4 // lambda capture mutability with explicit object parameter
6 void capture_by_value()
8   static constexpr int magic = 42;
9   auto f0 = [n = 0](this auto self){
10     n += magic;
11     return n;
12   };
13   auto f1 = [n = 0](this auto& self){
14     n += magic;
15     return n;
16   };
17   auto f2 = [n = 0](this auto&& self){
18     n += magic;
19     return n;
20   };
22   // passed by value, should still return a value equal to magic regardless
23   // of how many times it is called
24   if (f0 () != magic)
25     __builtin_abort ();
26   if (f0 () != magic)
27     __builtin_abort ();
28   // passed by reference, the returned value should increase by magic
29   // each time it is called
30   if (f1 () != magic)
31     __builtin_abort ();
32   if (f1 () != magic + magic)
33     __builtin_abort ();
34   if (f2 () != magic)
35     __builtin_abort ();
36   if (f2 () != magic + magic)
37     __builtin_abort ();
40 void capture_by_ref()
42   static constexpr int magic = 42;
43   int n0 = 0;
44   auto f0 = [&n0](this auto self){
45     n0 += magic;
46   };
47   int n1 = 0;
48   auto f1 = [&n1](this auto& self){
49     n1 += magic;
50   };
51   int n2 = 0;
52   auto f2 = [&n2](this auto&& self){
53     n2 += magic;
54   };
55   int n3 = 0;
56   auto f3 = [&n3](this auto const& self){
57     n3 += magic;
58   };
60   // all calls should mutate their capture, the capture is by reference
61   if (f0 (); n0 != magic)
62     __builtin_abort ();
63   if (f0 (); n0 != magic + magic)
64     __builtin_abort ();
66   if (f1 (); n1 != magic)
67     __builtin_abort ();
68   if (f1 (); n1 != magic + magic)
69     __builtin_abort ();
71   if (f2 (); n2 != magic)
72     __builtin_abort ();
73   if (f2 (); n2 != magic + magic)
74     __builtin_abort ();
76   if (f3 (); n3 != magic)
77     __builtin_abort ();
78   if (f3 (); n3 != magic + magic)
79     __builtin_abort ();
82 int main()
84   capture_by_value ();
85   capture_by_ref ();