File: cxx20-decomposition.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (185 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download | duplicates (6)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify -Wunused-variable %s

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

struct S {
  int i;
  int &j;
};

void check_category() {
  int a = 42;
  {
    auto [v, r] = S{1, a};
    (void)[ v, r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
  {
    auto [v, r] = S{1, a};
    (void)[&v, &r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
  {
    S s{1, a};
    const auto &[v, r] = s;
    (void)[ v, r ] {
      static_assert(is_same<decltype(v), const int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
  {
    S s{1, a};
    const auto &[v, r] = s;
    (void)[&v, &r ] {
      static_assert(is_same<decltype(v), const int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
}

void check_array() {
  int arr[2] = {42, 42};
  auto &[a, b] = arr;
  (void)[ a, &b ] {
    static_assert(is_same<decltype(a), int>);
    static_assert(is_same<decltype(b), int>);
  };
}

struct tuple {
  template <unsigned long I>
  decltype(auto) get() {
    if constexpr (I == 0) {
      return a;
    } else {
      return b;
    }
  }

  template <unsigned long I>
  decltype(auto) get() const {
    if constexpr (I == 0) {
      return a;
    } else {
      return b;
    }
  }

  int a = 0;
  int &b = a;
};

namespace std {

template <typename T>
struct tuple_size;

template <typename T>
struct tuple_size<T&> : tuple_size<T>{};

template <typename T>
requires requires { tuple_size<T>::value; }
struct tuple_size<const T> : tuple_size<T>{};

template <>
struct tuple_size<tuple> {
  static constexpr unsigned long value = 2;
};

template <unsigned long, typename T>
struct tuple_element;

template <>
struct tuple_element<0, tuple> {
  using type = int;
};

template <>
struct tuple_element<1, tuple> {
  using type = int &;
};

template <>
struct tuple_element<0, const tuple> {
  using type = int;
};

template <>
struct tuple_element<1, const tuple> {
  using type = const int &;
};
} // namespace std

void check_tuple_like() {
  tuple t;
  {
    auto [v, r] = t;
    (void)[ v, r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
  {
    auto &[v, r] = t;
    (void)[&v, &r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), int &>);
    };
  }
  {
    const auto &[v, r] = t;
    (void)[ v, r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), const int &>);
    };
  }
  {
    const auto &[v, r] = t;
    (void)[&v, &r ] {
      static_assert(is_same<decltype(v), int>);
      static_assert(is_same<decltype(r), const int &>);
    };
  }
}

namespace ODRUseTests {
  struct P { int a; int b; };
  void GH57826() {
    const auto [a, b] = P{1, 2}; //expected-note 2{{'b' declared here}} \
                                 //expected-note 3{{'a' declared here}}
    (void)[&](auto c) { return b + [&a] {
        return a;
    }(); }(0);
    (void)[&](auto c) { return b + [&a](auto) {
        return a;
    }(0); }(0);
    (void)[=](auto c) { return b + [&a](auto) {
        return a;
    }(0); }(0);
    (void)[&a,&b](auto c) { return b + [&a](auto) {
        return a;
    }(0); }(0);
    (void)[&a,&b](auto c) { return b + [a](auto) {
        return a;
    }(0); }(0);
    (void)[&a](auto c) { return b + [&a](auto) { // expected-error 2{{variable 'b' cannot be implicitly captured}} \
                                                 // expected-note 2{{lambda expression begins here}} \
                                                 // expected-note 4{{capture 'b'}}
        return a;
    }(0); }(0); // expected-note {{in instantiation}}
    (void)[&b](auto c) { return b + [](auto) {   // expected-note 3{{lambda expression begins here}} \
                                                 // expected-note 6{{capture 'a'}} \
                                                 // expected-note 6{{default capture}} \
                                                 // expected-note {{in instantiation}} \
                                                 // expected-note {{while substituting into a lambda}}
        return a;  // expected-error 3{{variable 'a' cannot be implicitly captured}}
    }(0); }(0); // expected-note 2{{in instantiation}}
  }
}