1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <memory>
10
11// allocator:
12// template <class... Args> void construct(pointer p, Args&&... args);
13
14// In C++20, parts of std::allocator<T> have been removed.
15// In C++17, they were deprecated.
16// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
17// REQUIRES: c++03 || c++11 || c++14 || c++17
18
19#include <memory>
20#include <cassert>
21
22#include "test_macros.h"
23#include "count_new.h"
24
25int A_constructed = 0;
26
27struct A {
28 int data;
29 A() { ++A_constructed; }
30
31 A(const A&) { ++A_constructed; }
32
33 explicit A(int) { ++A_constructed; }
34 A(int, int*) { ++A_constructed; }
35
36 ~A() { --A_constructed; }
37};
38
39int move_only_constructed = 0;
40
41#if TEST_STD_VER >= 11
42class move_only {
43 move_only(const move_only&) = delete;
44 move_only& operator=(const move_only&) = delete;
45
46public:
47 move_only(move_only&&) { ++move_only_constructed; }
48 move_only& operator=(move_only&&) { return *this; }
49
50 move_only() { ++move_only_constructed; }
51 ~move_only() { --move_only_constructed; }
52
53public:
54 int data; // unused other than to make sizeof(move_only) == sizeof(int).
55 // but public to suppress "-Wunused-private-field"
56};
57#endif // TEST_STD_VER >= 11
58
59int main(int, char**) {
60 globalMemCounter.reset();
61 {
62 std::allocator<A> a;
63 assert(globalMemCounter.checkOutstandingNewEq(0));
64 assert(A_constructed == 0);
65
66 globalMemCounter.last_new_size = 0;
67 A* ap = a.allocate(n: 3);
68 DoNotOptimize(ap);
69 assert(globalMemCounter.checkOutstandingNewEq(1));
70 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
71 assert(A_constructed == 0);
72
73 a.construct(p: ap);
74 assert(globalMemCounter.checkOutstandingNewEq(1));
75 assert(A_constructed == 1);
76
77 a.destroy(p: ap);
78 assert(globalMemCounter.checkOutstandingNewEq(1));
79 assert(A_constructed == 0);
80
81 a.construct(p: ap, args: A());
82 assert(globalMemCounter.checkOutstandingNewEq(1));
83 assert(A_constructed == 1);
84
85 a.destroy(p: ap);
86 assert(globalMemCounter.checkOutstandingNewEq(1));
87 assert(A_constructed == 0);
88
89 a.construct(p: ap, args: 5);
90 assert(globalMemCounter.checkOutstandingNewEq(1));
91 assert(A_constructed == 1);
92
93 a.destroy(p: ap);
94 assert(globalMemCounter.checkOutstandingNewEq(1));
95 assert(A_constructed == 0);
96
97 a.construct(p: ap, args: 5, args: (int*)0);
98 assert(globalMemCounter.checkOutstandingNewEq(1));
99 assert(A_constructed == 1);
100
101 a.destroy(p: ap);
102 assert(globalMemCounter.checkOutstandingNewEq(1));
103 assert(A_constructed == 0);
104
105 a.deallocate(p: ap, t: 3);
106 DoNotOptimize(ap);
107 assert(globalMemCounter.checkOutstandingNewEq(0));
108 assert(A_constructed == 0);
109 }
110#if TEST_STD_VER >= 11
111 {
112 std::allocator<move_only> a;
113 assert(globalMemCounter.checkOutstandingNewEq(0));
114 assert(move_only_constructed == 0);
115
116 globalMemCounter.last_new_size = 0;
117 move_only* ap = a.allocate(3);
118 DoNotOptimize(ap);
119 assert(globalMemCounter.checkOutstandingNewEq(1));
120 assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
121 assert(move_only_constructed == 0);
122
123 a.construct(ap);
124 assert(globalMemCounter.checkOutstandingNewEq(1));
125 assert(move_only_constructed == 1);
126
127 a.destroy(ap);
128 assert(globalMemCounter.checkOutstandingNewEq(1));
129 assert(move_only_constructed == 0);
130
131 a.construct(ap, move_only());
132 assert(globalMemCounter.checkOutstandingNewEq(1));
133 assert(move_only_constructed == 1);
134
135 a.destroy(ap);
136 assert(globalMemCounter.checkOutstandingNewEq(1));
137 assert(move_only_constructed == 0);
138
139 a.deallocate(ap, 3);
140 DoNotOptimize(ap);
141 assert(globalMemCounter.checkOutstandingNewEq(0));
142 assert(move_only_constructed == 0);
143 }
144#endif
145
146 return 0;
147}
148

source code of libcxx/test/libcxx/depr/depr.default.allocator/allocator.members/construct.cxx20.pass.cpp