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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <memory>
12 
13 // template <class ForwardIt>
14 // constexpr void destroy(ForwardIt, ForwardIt);
15 
16 #include <memory>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 
22 struct Counted {
23     int* counter_;
24     TEST_CONSTEXPR Counted(int* counter) : counter_(counter) { ++*counter_; }
25     TEST_CONSTEXPR Counted(Counted const& other) : counter_(other.counter_) { ++*counter_; }
26     TEST_CONSTEXPR_CXX20 ~Counted() { --*counter_; }
27     friend void operator&(Counted) = delete;
28 };
29 
30 TEST_CONSTEXPR_CXX20 bool test()
31 {
32     using Alloc = std::allocator<Counted>;
33     int counter = 0;
34     int const N = 5;
35     Alloc alloc;
36     Counted* pool = std::allocator_traits<Alloc>::allocate(alloc, N);
37 
38     for (Counted* p = pool; p != pool + N; ++p)
39         std::allocator_traits<Alloc>::construct(alloc, p, &counter);
40     assert(counter == 5);
41 
42     std::destroy(pool, pool + 1);
43     assert(counter == 4);
44 
45     std::destroy(forward_iterator<Counted*>(pool + 1), forward_iterator<Counted*>(pool + 5));
46     assert(counter == 0);
47 
48     std::allocator_traits<Alloc>::deallocate(alloc, pool, N);
49 
50     return true;
51 }
52 
53 int main(int, char**)
54 {
55     test();
56 #if TEST_STD_VER > 17
57     static_assert(test());
58 #endif
59     return 0;
60 }
61