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 
10 // struct destroying_delete_t {
11 //   explicit destroying_delete_t() = default;
12 // };
13 // inline constexpr destroying_delete_t destroying_delete{};
14 
15 // UNSUPPORTED: c++03, c++11, c++14, c++17
16 
17 #include <new>
18 
19 #include <cassert>
20 #include "test_macros.h"
21 
22 struct A {
23   void *data;
24   A();
25   ~A();
26 
27   static A* New();
28   void operator delete(A*, std::destroying_delete_t);
29 };
30 
31 bool A_constructed = false;
32 bool A_destroyed = false;
33 bool A_destroying_deleted = false;
34 
A()35 A::A() {
36   A_constructed = true;
37 }
38 
~A()39 A::~A() {
40   A_destroyed = true;
41 }
42 
New()43 A* A::New() {
44   return new(::operator new(sizeof(A))) A();
45 }
46 
operator delete(A * a,std::destroying_delete_t)47 void A::operator delete(A* a, std::destroying_delete_t) {
48   A_destroying_deleted = true;
49   ::operator delete(a);
50 }
51 
52 // Only test the definition of the library feature-test macro when the compiler
53 // supports the feature -- otherwise we don't define the library feature-test
54 // macro.
55 #if defined(__cpp_impl_destroying_delete)
56 #  if !defined(__cpp_lib_destroying_delete)
57 #    error "Expected __cpp_lib_destroying_delete to be defined"
58 #  elif __cpp_lib_destroying_delete < 201806L
59 #    error "Unexpected value of __cpp_lib_destroying_delete"
60 #  endif
61 #else
62 #  if defined(__cpp_lib_destroying_delete)
63 #    error "The library feature-test macro for destroying delete shouldn't be defined when the compiler doesn't support the language feature"
64 #  endif
65 #endif
66 
main(int,char **)67 int main(int, char**) {
68   // Ensure that we call the destroying delete and not the destructor.
69   A* ap = A::New();
70   assert(A_constructed);
71   delete ap;
72   assert(!A_destroyed);
73   assert(A_destroying_deleted);
74   return 0;
75 }
76