destroying_delete_t.pass.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // struct destroying_delete_t {
  10. // explicit destroying_delete_t() = default;
  11. // };
  12. // inline constexpr destroying_delete_t destroying_delete{};
  13. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  14. // UNSUPPORTED: apple-clang-9, apple-clang-10
  15. // UNSUPPORTED: clang-6, clang-7
  16. #include <new>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. struct A {
  20. void *data;
  21. A();
  22. ~A();
  23. static A* New();
  24. void operator delete(A*, std::destroying_delete_t);
  25. };
  26. bool A_constructed = false;
  27. bool A_destroyed = false;
  28. bool A_destroying_deleted = false;
  29. A::A() {
  30. A_constructed = true;
  31. }
  32. A::~A() {
  33. A_destroyed = true;
  34. }
  35. A* A::New() {
  36. return new(::operator new(sizeof(A))) A();
  37. }
  38. void A::operator delete(A* a, std::destroying_delete_t) {
  39. A_destroying_deleted = true;
  40. ::operator delete(a);
  41. }
  42. // Only test the definition of the library feature-test macro when the compiler
  43. // supports the feature -- otherwise we don't define the library feature-test
  44. // macro.
  45. #if defined(__cpp_impl_destroying_delete)
  46. # if !defined(__cpp_lib_destroying_delete)
  47. # error "Expected __cpp_lib_destroying_delete to be defined"
  48. # elif __cpp_lib_destroying_delete < 201806L
  49. # error "Unexpected value of __cpp_lib_destroying_delete"
  50. # endif
  51. #else
  52. # if defined(__cpp_lib_destroying_delete)
  53. # error "The library feature-test macro for destroying delete shouldn't be defined when the compiler doesn't support the language feature"
  54. # endif
  55. #endif
  56. int main() {
  57. // Ensure that we call the destroying delete and not the destructor.
  58. A* ap = A::New();
  59. assert(A_constructed);
  60. delete ap;
  61. assert(!A_destroyed);
  62. assert(A_destroying_deleted);
  63. }