|
17 | 17 | #include <any>
|
18 | 18 | #include <cassert>
|
19 | 19 | #include <cstddef>
|
20 |
| -#include <memory> |
21 | 20 | #include <new>
|
22 |
| -#include <type_traits> |
23 |
| -#include <utility> |
24 | 21 |
|
25 |
| -#include "test_macros.h" |
| 22 | +// Make sure we don't fit in std::any's SBO |
| 23 | +int allocated_count = 0; |
| 24 | +int constructed_count = 0; |
26 | 25 |
|
| 26 | +struct Large { |
| 27 | + Large() { ++constructed_count; } |
27 | 28 |
|
28 |
| -// Make sure we don't fit in std::any's SBO |
29 |
| -struct Large { char big[sizeof(std::any) + 1]; }; |
| 29 | + Large(const Large&) { ++constructed_count; } |
30 | 30 |
|
31 |
| -// Make sure we fit in std::any's SBO |
32 |
| -struct Small { }; |
33 |
| - |
34 |
| -bool Large_was_allocated = false; |
35 |
| -bool Large_was_constructed = false; |
36 |
| -bool Large_was_destroyed = false; |
37 |
| -bool Large_was_deallocated = false; |
38 |
| - |
39 |
| -bool Small_was_constructed = false; |
40 |
| -bool Small_was_destroyed = false; |
41 |
| - |
42 |
| -template <> |
43 |
| -struct std::allocator<Large> { |
44 |
| - using value_type = Large; |
45 |
| - using size_type = std::size_t; |
46 |
| - using difference_type = std::ptrdiff_t; |
47 |
| - using propagate_on_container_move_assignment = std::true_type; |
48 |
| - using is_always_equal = std::true_type; |
49 |
| - |
50 |
| - Large* allocate(std::size_t n) { |
51 |
| - Large_was_allocated = true; |
52 |
| - return static_cast<Large*>(::operator new(n * sizeof(Large))); |
53 |
| - } |
| 31 | + ~Large() { --constructed_count; } |
54 | 32 |
|
55 |
| - template <typename... Args> |
56 |
| - void construct(Large* p, Args&&... args) { |
57 |
| - new (p) Large(std::forward<Args>(args)...); |
58 |
| - Large_was_constructed = true; |
59 |
| - } |
| 33 | + char big[sizeof(std::any) + 1]; |
60 | 34 |
|
61 |
| - void destroy(Large* p) { |
62 |
| - p->~Large(); |
63 |
| - Large_was_destroyed = true; |
| 35 | + static void* operator new(size_t n) { |
| 36 | + ++allocated_count; |
| 37 | + return ::operator new(n); |
64 | 38 | }
|
65 | 39 |
|
66 |
| - void deallocate(Large* p, std::size_t) { |
67 |
| - Large_was_deallocated = true; |
68 |
| - return ::operator delete(p); |
| 40 | + static void operator delete(void* ptr) { |
| 41 | + --allocated_count; |
| 42 | + ::operator delete(ptr); |
69 | 43 | }
|
70 | 44 | };
|
71 | 45 |
|
72 |
| -template <> |
73 |
| -struct std::allocator<Small> { |
74 |
| - using value_type = Small; |
75 |
| - using size_type = std::size_t; |
76 |
| - using difference_type = std::ptrdiff_t; |
77 |
| - using propagate_on_container_move_assignment = std::true_type; |
78 |
| - using is_always_equal = std::true_type; |
79 |
| - |
80 |
| - Small* allocate(std::size_t) { |
81 |
| - assert(false); |
82 |
| - return nullptr; |
83 |
| - } |
| 46 | +// Make sure we fit in std::any's SBO |
| 47 | +struct Small { |
| 48 | + Small() { ++constructed_count; } |
84 | 49 |
|
85 |
| - template <typename... Args> |
86 |
| - void construct(Small* p, Args&&... args) { |
87 |
| - new (p) Small(std::forward<Args>(args)...); |
88 |
| - Small_was_constructed = true; |
89 |
| - } |
| 50 | + Small(const Small&) { ++constructed_count; } |
| 51 | + |
| 52 | + ~Small() { --constructed_count; } |
90 | 53 |
|
91 |
| - void destroy(Small* p) { |
92 |
| - p->~Small(); |
93 |
| - Small_was_destroyed = true; |
| 54 | + static void* operator new(size_t n) { |
| 55 | + ++allocated_count; |
| 56 | + return ::operator new(n); |
94 | 57 | }
|
95 | 58 |
|
96 |
| - void deallocate(Small*, std::size_t) { assert(false); } |
| 59 | + static void operator delete(void* ptr) { |
| 60 | + --allocated_count; |
| 61 | + ::operator delete(ptr); |
| 62 | + } |
97 | 63 | };
|
98 | 64 |
|
99 | 65 | int main(int, char**) {
|
100 | 66 | // Test large types
|
101 | 67 | {
|
102 |
| - { |
103 |
| - std::any a = Large(); |
104 |
| - (void)a; |
105 |
| - |
106 |
| - assert(Large_was_allocated); |
107 |
| - assert(Large_was_constructed); |
108 |
| - } |
109 |
| - |
110 |
| - assert(Large_was_destroyed); |
111 |
| - assert(Large_was_deallocated); |
| 68 | + [[maybe_unused]] std::any a = Large(); |
| 69 | + assert(constructed_count == 1); |
112 | 70 | }
|
| 71 | + assert(allocated_count == 0); |
| 72 | + assert(constructed_count == 0); |
113 | 73 |
|
114 | 74 | // Test small types
|
115 | 75 | {
|
116 |
| - { |
117 |
| - std::any a = Small(); |
118 |
| - (void)a; |
119 |
| - |
120 |
| - assert(Small_was_constructed); |
121 |
| - } |
122 |
| - |
123 |
| - assert(Small_was_destroyed); |
| 76 | + [[maybe_unused]] std::any a = Small(); |
| 77 | + assert(constructed_count == 1); |
124 | 78 | }
|
| 79 | + assert(allocated_count == 0); |
| 80 | + assert(constructed_count == 0); |
125 | 81 |
|
126 | 82 | return 0;
|
127 | 83 | }
|
0 commit comments