destroy.pass.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <memory>
  10. // template <class ForwardIt>
  11. // void destroy(ForwardIt, ForwardIt);
  12. #include <memory>
  13. #include <cstdlib>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. struct Counted {
  18. static int count;
  19. static void reset() { count = 0; }
  20. Counted() { ++count; }
  21. Counted(Counted const&) { ++count; }
  22. ~Counted() { --count; }
  23. friend void operator&(Counted) = delete;
  24. };
  25. int Counted::count = 0;
  26. int main(int, char**)
  27. {
  28. using It = forward_iterator<Counted*>;
  29. const int N = 5;
  30. alignas(Counted) char pool[sizeof(Counted)*N] = {};
  31. Counted* p = (Counted*)pool;
  32. std::uninitialized_fill(p, p+N, Counted());
  33. assert(Counted::count == 5);
  34. std::destroy(p, p+1);
  35. p += 1;
  36. assert(Counted::count == 4);
  37. std::destroy(It(p), It(p + 4));
  38. assert(Counted::count == 0);
  39. return 0;
  40. }