Skip to content

Commit c8fbb01

Browse files
committed
Use std::destroy_at/std::destroy/std::destroy_n (#66)
1 parent 873513b commit c8fbb01

File tree

5 files changed

+17
-51
lines changed

5 files changed

+17
-51
lines changed

include/cpp-sort/detail/fixed_size_list.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2022 Morwenn
2+
* Copyright (c) 2020-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_DETAIL_FIXED_SIZE_LIST_H_
@@ -92,7 +92,7 @@ namespace detail
9292
auto destroy_node_contents(NodeType* node)
9393
-> void
9494
{
95-
detail::destroy_at(&(node->*Ptr));
95+
std::destroy_at(&(node->*Ptr));
9696
}
9797

9898
////////////////////////////////////////////////////////////
@@ -170,10 +170,7 @@ namespace detail
170170
~fixed_size_list_node_pool()
171171
{
172172
// Destroy the nodes
173-
node_type* ptr = buffer_.get();
174-
for (std::ptrdiff_t n = 0 ; n < capacity_ ; ++n, ++ptr) {
175-
detail::destroy_at(ptr);
176-
}
173+
std::destroy_n(buffer_.get(), capacity_);
177174
}
178175

179176
////////////////////////////////////////////////////////////

include/cpp-sort/detail/immovable_vector.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022 Morwenn
2+
* Copyright (c) 2021-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_DETAIL_IMMOVABLE_VECTOR_H_
@@ -9,11 +9,11 @@
99
// Headers
1010
////////////////////////////////////////////////////////////
1111
#include <cstddef>
12+
#include <memory>
1213
#include <new>
1314
#include <utility>
1415
#include <cpp-sort/utility/iter_move.h>
1516
#include "config.h"
16-
#include "memory.h"
1717

1818
namespace cppsort
1919
{
@@ -59,7 +59,7 @@ namespace detail
5959
~immovable_vector()
6060
{
6161
// Destroy the constructed elements
62-
detail::destroy(memory_, end_);
62+
std::destroy(memory_, end_);
6363

6464
// Free the allocated memory
6565
#ifdef __cpp_sized_deallocation
@@ -115,7 +115,7 @@ namespace detail
115115
-> void
116116
{
117117
// Destroy the constructed elements
118-
detail::destroy(memory_, end_);
118+
std::destroy(memory_, end_);
119119

120120
// Ensure the new size is zero
121121
end_ = memory_;
@@ -143,7 +143,7 @@ namespace detail
143143
}
144144
} catch (...) {
145145
// Cleanup
146-
detail::destroy(end_, writer);
146+
std::destroy(end_, writer);
147147
throw;
148148
}
149149
end_ = writer;

include/cpp-sort/detail/memory.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2022 Morwenn
2+
* Copyright (c) 2016-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55

@@ -29,37 +29,6 @@ namespace cppsort
2929
{
3030
namespace detail
3131
{
32-
////////////////////////////////////////////////////////////
33-
// C++17 std::destroy and friends
34-
35-
template<typename T>
36-
auto destroy_at(T* ptr)
37-
-> void
38-
{
39-
// TODO: implement if needed
40-
static_assert(not std::is_array<T>::value, "destroy_at() does no handle arrays");
41-
ptr->~T();
42-
}
43-
44-
template<typename ForwardIterator>
45-
auto destroy(ForwardIterator first, ForwardIterator last)
46-
-> void
47-
{
48-
for (; first != last; ++first) {
49-
detail::destroy_at(std::addressof(*first));
50-
}
51-
}
52-
53-
template<typename ForwardIterator, typename Size>
54-
auto destroy_n(ForwardIterator first, Size n)
55-
-> void
56-
{
57-
for (; n > 0; --n) {
58-
detail::destroy_at(std::addressof(*first));
59-
++first;
60-
}
61-
}
62-
6332
////////////////////////////////////////////////////////////
6433
// Deleter for ::operator new(std::size_t)
6534

@@ -122,7 +91,7 @@ namespace detail
12291
constexpr auto operator()(T* pointer) noexcept
12392
-> void
12493
{
125-
detail::destroy_n(pointer, size);
94+
std::destroy_n(pointer, size);
12695
}
12796

12897
// Number of allocated objects to destroy

include/cpp-sort/detail/slabsort.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022 Morwenn
2+
* Copyright (c) 2021-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_DETAIL_SLABSORT_H_
@@ -9,6 +9,7 @@
99
// Headers
1010
////////////////////////////////////////////////////////////
1111
#include <iterator>
12+
#include <memory>
1213
#include <utility>
1314
#include <vector>
1415
#include <cpp-sort/comparators/flip.h>
@@ -24,7 +25,6 @@
2425
#include "iterator_traits.h"
2526
#include "lower_bound.h"
2627
#include "melsort.h"
27-
#include "memory.h"
2828
#include "stable_partition.h"
2929
#include "nth_element.h"
3030

@@ -203,7 +203,7 @@ namespace detail
203203
for (auto it = list.begin(), end = list.end() ; it != end ; ++it) {
204204
auto node = it.base();
205205
auto value_it = node->it;
206-
detail::destroy_at(&node->it);
206+
std::destroy_at(&node->it);
207207
::new(&node->value) rvalue_type(iter_move(value_it));
208208
}
209209
list.set_node_destructor(destroy_node_contents<rvalue_type, node_type, &node_type::value>);

tests/testing-tools/test_vector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021 Morwenn
2+
* Copyright (c) 2021-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_TESTSUITE_TEST_VECTOR_H_
@@ -10,10 +10,10 @@
1010
////////////////////////////////////////////////////////////
1111
#include <cstddef>
1212
#include <iterator>
13+
#include <memory>
1314
#include <new>
1415
#include <stdexcept>
1516
#include <utility>
16-
#include <cpp-sort/detail/memory.h>
1717

1818
////////////////////////////////////////////////////////////
1919
// Vector with a tiny size
@@ -273,7 +273,7 @@ class test_vector
273273
~test_vector()
274274
{
275275
// Destroy the constructed elements
276-
cppsort::detail::destroy(memory_, end_);
276+
std::destroy(memory_, end_);
277277
// Free the allocated memory
278278
::operator delete(memory_);
279279
}
@@ -330,7 +330,7 @@ class test_vector
330330
-> void
331331
{
332332
// Destroy the constructed elements
333-
cppsort::detail::destroy(memory_, end_);
333+
std::destroy(memory_, end_);
334334
// Ensure the new size is zero
335335
end_ = memory_;
336336
}

0 commit comments

Comments
 (0)