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