[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx20-decomposition.cpp
bloba99766f5775b2259495220d791b2a4ec0c28dd90
1 // RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
2 // expected-no-diagnostics
4 template <typename, typename>
5 constexpr bool is_same = false;
6 template <typename T>
7 constexpr bool is_same<T, T> = true;
9 struct S {
10 int i;
11 int &j;
14 void check_category() {
15 int a = 42;
17 auto [v, r] = S{1, a};
18 (void)[ v, r ] {
19 static_assert(is_same<decltype(v), int>);
20 static_assert(is_same<decltype(r), int &>);
24 auto [v, r] = S{1, a};
25 (void)[&v, &r ] {
26 static_assert(is_same<decltype(v), int>);
27 static_assert(is_same<decltype(r), int &>);
31 S s{1, a};
32 const auto &[v, r] = s;
33 (void)[ v, r ] {
34 static_assert(is_same<decltype(v), const int>);
35 static_assert(is_same<decltype(r), int &>);
39 S s{1, a};
40 const auto &[v, r] = s;
41 (void)[&v, &r ] {
42 static_assert(is_same<decltype(v), const int>);
43 static_assert(is_same<decltype(r), int &>);
48 void check_array() {
49 int arr[2] = {42, 42};
50 auto &[a, b] = arr;
51 (void)[ a, &b ] {
52 static_assert(is_same<decltype(a), int>);
53 static_assert(is_same<decltype(b), int>);
57 struct tuple {
58 template <unsigned long I>
59 decltype(auto) get() {
60 if constexpr (I == 0) {
61 return a;
62 } else {
63 return b;
67 template <unsigned long I>
68 decltype(auto) get() const {
69 if constexpr (I == 0) {
70 return a;
71 } else {
72 return b;
76 int a = 0;
77 int &b = a;
80 namespace std {
82 template <typename T>
83 struct tuple_size {
84 static constexpr unsigned long value = 2;
87 template <unsigned long, typename T>
88 struct tuple_element;
90 template <>
91 struct tuple_element<0, tuple> {
92 using type = int;
95 template <>
96 struct tuple_element<1, tuple> {
97 using type = int &;
100 template <>
101 struct tuple_element<0, const tuple> {
102 using type = int;
105 template <>
106 struct tuple_element<1, const tuple> {
107 using type = const int &;
109 } // namespace std
111 void check_tuple_like() {
112 tuple t;
114 auto [v, r] = t;
115 (void)[ v, r ] {
116 static_assert(is_same<decltype(v), int>);
117 static_assert(is_same<decltype(r), int &>);
121 auto &[v, r] = t;
122 (void)[&v, &r ] {
123 static_assert(is_same<decltype(v), int>);
124 static_assert(is_same<decltype(r), int &>);
128 const auto &[v, r] = t;
129 (void)[ v, r ] {
130 static_assert(is_same<decltype(v), int>);
131 static_assert(is_same<decltype(r), const int &>);
135 const auto &[v, r] = t;
136 (void)[&v, &r ] {
137 static_assert(is_same<decltype(v), int>);
138 static_assert(is_same<decltype(r), const int &>);