destroy_n.pass.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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, class Size>
  11. // ForwardIt destroy_n(ForwardIt, Size s);
  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. Counted* np = std::destroy_n(p, 1);
  35. assert(np == p+1);
  36. assert(Counted::count == 4);
  37. p += 1;
  38. It it = std::destroy_n(It(p), 4);
  39. assert(it == It(p+4));
  40. assert(Counted::count == 0);
  41. return 0;
  42. }