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