[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx20-p0388-unbound-ary.cpp
blobf2d5cabad235d0bbf9092f626fa6ecb536a812a3
1 // RUN: %clang_cc1 -std=c++20 -verify %s
2 // RUN: %clang_cc1 -std=c++17 -verify %s
4 // p0388 conversions to unbounded array
5 // dcl.init.list/3
7 namespace One {
8 int ga[1];
10 auto &frob1() {
11 int(&r1)[] = ga;
12 #if __cplusplus < 202002
13 // expected-error@-2{{cannot bind to a value of unrelated type}}
14 #endif
16 return r1;
19 auto &frob2(int (&arp)[1]) {
20 int(&r2)[] = arp;
21 #if __cplusplus < 202002
22 // expected-error@-2{{cannot bind to a value of unrelated type}}
23 #endif
25 return r2;
27 } // namespace One
29 namespace Two {
30 int ga[1];
32 auto *frob1() {
33 int(*r1)[] = &ga;
34 #if __cplusplus < 202002
35 // expected-error@-2{{with an rvalue of type}}
36 #endif
38 return r1;
41 auto *frob2(int (*arp)[1]) {
42 int(*r2)[] = arp;
43 #if __cplusplus < 202002
44 // expected-error@-2{{with an lvalue of type}}
45 #endif
47 return r2;
49 } // namespace Two
51 namespace Four {
52 using Inc = int[2];
53 using Mat = Inc[1];
54 Mat *ga[2];
56 auto *frob1() {
57 Inc(*const(*r1)[])[] = &ga;
58 #if __cplusplus < 202002
59 // expected-error@-2{{with an rvalue of type}}
60 #else
61 // missing a required 'const'
62 Inc(*(*r2)[])[] = &ga; // expected-error{{cannot initialize}}
63 #endif
65 return r1;
68 auto *frob2(Mat *(*arp)[1]) {
69 Inc(*const(*r2)[])[] = arp;
70 #if __cplusplus < 202002
71 // expected-error@-2{{with an lvalue of type}}
72 #else
73 Inc(*(*r3)[])[] = arp; // expected-error{{cannot initialize}}
74 #endif
76 return r2;
79 } // namespace Four
81 namespace Five {
82 // from the paper
83 char (&b(int(&&)[]))[1]; // #1
84 char (&b(long(&&)[]))[2]; // #2
85 char (&b(int(&&)[1]))[3]; // #3
86 char (&b(long(&&)[1]))[4]; // #4
87 char (&b(int(&&)[2]))[5]; // #5
88 #if __cplusplus < 202002
89 // expected-note@-6{{cannot convert initializer}}
90 // expected-note@-6{{cannot convert initializer}}
91 // expected-note@-6{{too many initializers}}
92 // expected-note@-6{{too many initializers}}
93 // expected-note@-6{{too many initializers}}
94 #endif
96 void f() {
97 static_assert(sizeof(b({1})) == 3);
98 static_assert(sizeof(b({1, 2})) == 5);
99 static_assert(sizeof(b({1, 2, 3})) == 1);
100 #if __cplusplus < 202002
101 // expected-error@-2{{no matching function}}
102 #endif
104 } // namespace Five
106 #if __cplusplus >= 202002
107 namespace Six {
108 // from over.ics.rank 3.1
109 char (&f(int(&&)[]))[1]; // #1
110 char (&f(double(&&)[]))[2]; // #2
111 char (&f(int(&&)[2]))[3]; // #3
113 void toto() {
114 // Calls #1: Better than #2 due to conversion, better than #3 due to bounds
115 static_assert(sizeof(f({1})) == 1);
117 // Calls #2: Identity conversion is better than floating-integral conversion
118 static_assert(sizeof(f({1.0})) == 2);
120 // Calls #2: Identity conversion is better than floating-integral conversion
121 static_assert(sizeof(f({1.0, 2.0})) == 2);
123 // Calls #3: Converting to array of known bound is better than to unknown
124 // bound, and an identity conversion is better than
125 // floating-integral conversion
126 static_assert(sizeof(f({1, 2})) == 3);
129 } // namespace Six
131 namespace Seven {
133 char (&f(int(&&)[]))[1]; // #1
134 char (&f(double(&&)[1]))[2]; // #2
136 void quux() {
137 // Calls #2, float-integral conversion rather than create zero-sized array
138 static_assert(sizeof(f({})) == 2);
141 } // namespace Seven
143 namespace Eight {
145 // brace-elision is not a thing here:
146 struct A {
147 int x, y;
150 char (&f1(int(&&)[]))[1]; // #1
151 char (&f1(A(&&)[]))[2]; // #2
153 void g1() {
154 // pick #1, even though that is more elements than #2
155 // 6 ints, as opposed to 3 As
156 static_assert(sizeof(f1({1, 2, 3, 4, 5, 6})) == 1);
159 void f2(A(&&)[]); // expected-note{{candidate function not viable}}
160 void g2() {
161 f2({1, 2, 3, 4, 5, 6}); // expected-error{{no matching function}}
164 void f3(A(&&)[]);
165 void g3() {
166 auto &f = f3;
168 f({1, 2, 3, 4, 5, 6}); // OK! We're coercing to an already-selected function
171 } // namespace Eight
173 #endif