Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/constexpr-new1.C

raw
1 // C++26 P2747R2 - constexpr placement new
2 // { dg-do compile { target c++26 } }
3
4 #include "../cpp2a/construct_at.h"
5
6 struct S {
7 constexpr S () : a (42), b (43) {}
8 constexpr S (int c, int d) : a (c), b (d) {}
9 int a, b;
10 };
11 struct T {
12 int a, b;
13 };
14
15 constexpr bool
16 foo ()
17 {
18 std::allocator<int> a;
19 auto b = a.allocate (3);
20 ::new (b) int ();
21 ::new (b + 1) int (1);
22 ::new (b + 2) int {2};
23 if (b[0] != 0 || b[1] != 1 || b[2] != 2)
24 return false;
25 a.deallocate (b, 3);
26 std::allocator<S> c;
27 auto d = c.allocate (4);
28 ::new (d) S;
29 ::new (d + 1) S ();
30 ::new (d + 2) S (7, 8);
31 ::new (d + 3) S { 9, 10 };
32 if (d[0].a != 42 || d[0].b != 43
33 || d[1].a != 42 || d[1].b != 43
34 || d[2].a != 7 || d[2].b != 8
35 || d[3].a != 9 || d[3].b != 10)
36 return false;
37 d[0].~S ();
38 d[1].~S ();
39 d[2].~S ();
40 d[3].~S ();
41 c.deallocate (d, 4);
42 std::allocator<T> e;
43 auto f = e.allocate (3);
44 ::new (f) T ();
45 ::new (f + 1) T (7, 8);
46 ::new (f + 2) T { .a = 9, .b = 10 };
47 if (f[0].a != 0 || f[0].b != 0
48 || f[1].a != 7 || f[1].b != 8
49 || f[2].a != 9 || f[2].b != 10)
50 return false;
51 f[0].~T ();
52 f[1].~T ();
53 f[2].~T ();
54 e.deallocate (f, 3);
55 auto g = a.allocate (3);
56 new (g) int[] {1, 2, 3};
57 if (g[0] != 1 || g[1] != 2 || g[2] != 3)
58 return false;
59 new (g) int[] {4, 5};
60 if (g[0] != 4 || g[1] != 5)
61 return false;
62 a.deallocate (g, 3);
63 return true;
64 }
65
66 static_assert (foo ());