aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/lambda-scope1.C
blob: 000979e307786f25fca090937967e2becfc20b43 (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
// P2036R3 - Change scope of lambda trailing-return-type
// PR c++/102610
// { dg-do compile { target c++23 } }

template <typename T, typename U>
constexpr bool is_same = false;

template <typename T>
constexpr bool is_same<T, T> = true;

struct S {
  void foo () {
    auto counter1 = [j=0]() mutable -> decltype(j) {
      return j++;
    };
    auto counter2 = [j=0, o=0, k=0, e=0]() mutable -> decltype(j) {
      return j + o + k + e;
    };
  }
};

// [expr.prim.id.unqual]/3.2
void
f ()
{
  float x, &r = x;

  [=]() -> decltype((x)) {      // lambda returns float const& because this lambda is not mutable and
                                // x is an lvalue
    decltype(x) y1;             // y1 has type float
    decltype((x)) y2 = y1;      // y2 has type float const&
    decltype(r) r1 = y1;        // r1 has type float&
    decltype((r)) r2 = y2;      // r2 has type float const&
    return y2;
  };

  [=](decltype((x)) y) {
    decltype((x)) z = x;        // OK, y has type float&, z has type float const&
    static_assert(is_same<float&, decltype((y))>);
    static_assert(is_same<const float&, decltype((z))>);
  };

  [=] {
    [](decltype((x)) y) {     // OK, lambda takes a parameter of type float const&
    };

    [x=1](decltype((x)) y) {
      decltype((x)) z = x;      // OK, y has type int&, z has type int const&
      // FIXME We don't handle nested lambdas yet?
      //static_assert(is_same<int&, decltype((y))>);
      static_assert(is_same<const int&, decltype((z))>);
    };
  };

  [x=1](decltype((x)) y) {
    decltype((x)) z = x;
    static_assert(is_same<int&, decltype((y))>);
    static_assert(is_same<const int&, decltype((z))>);
  };
}

void
ok ()
{
  auto counter1 = [j=0]() mutable -> decltype(j) {
    static_assert(is_same<int&, decltype((j))>);
    return j++;
  };

  auto l = [j=0]() -> decltype(j) {
    static_assert(is_same<const int&, decltype((j))>);
    return j;
  };

  int y;
  [=] -> decltype((y)) {
    return y;
  };
}

void
foo ()
{
  int x = [x](int y[sizeof x]){return sizeof x;}(0);
}