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