aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda8.C
blob: a068941413a3af58c033e73fa706af88c140d458 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// P0847R7
// { dg-do run { target c++23 } }

// lambda capture mutability with explicit object parameter

void capture_by_value()
{
  static constexpr int magic = 42;
  auto f0 = [n = 0](this auto self){
    n += magic;
    return n;
  };
  auto f1 = [n = 0](this auto& self){
    n += magic;
    return n;
  };
  auto f2 = [n = 0](this auto&& self){
    n += magic;
    return n;
  };

  // passed by value, should still return a value equal to magic regardless
  // of how many times it is called
  if (f0 () != magic)
    __builtin_abort ();
  if (f0 () != magic)
    __builtin_abort ();
  // passed by reference, the returned value should increase by magic
  // each time it is called
  if (f1 () != magic)
    __builtin_abort ();
  if (f1 () != magic + magic)
    __builtin_abort ();
  if (f2 () != magic)
    __builtin_abort ();
  if (f2 () != magic + magic)
    __builtin_abort ();
}

void capture_by_ref()
{
  static constexpr int magic = 42;
  int n0 = 0;
  auto f0 = [&n0](this auto self){
    n0 += magic;
  };
  int n1 = 0;
  auto f1 = [&n1](this auto& self){
    n1 += magic;
  };
  int n2 = 0;
  auto f2 = [&n2](this auto&& self){
    n2 += magic;
  };
  int n3 = 0;
  auto f3 = [&n3](this auto const& self){
    n3 += magic;
  };

  // all calls should mutate their capture, the capture is by reference
  if (f0 (); n0 != magic)
    __builtin_abort ();
  if (f0 (); n0 != magic + magic)
    __builtin_abort ();

  if (f1 (); n1 != magic)
    __builtin_abort ();
  if (f1 (); n1 != magic + magic)
    __builtin_abort ();

  if (f2 (); n2 != magic)
    __builtin_abort ();
  if (f2 (); n2 != magic + magic)
    __builtin_abort ();

  if (f3 (); n3 != magic)
    __builtin_abort ();
  if (f3 (); n3 != magic + magic)
    __builtin_abort ();
}

int main()
{
  capture_by_value ();
  capture_by_ref ();
}