brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 95d2c41 Raw
75 lines · cpp
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-exception6//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++1715 16#include <new>17 18#include <cassert>19#include "test_macros.h"20 21struct A {22  void *data;23  A();24  ~A();25 26  static A* New();27  void operator delete(A*, std::destroying_delete_t);28};29 30bool A_constructed = false;31bool A_destroyed = false;32bool A_destroying_deleted = false;33 34A::A() {35  A_constructed = true;36}37 38A::~A() {39  A_destroyed = true;40}41 42A* A::New() {43  return new(::operator new(sizeof(A))) A();44}45 46void 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 compiler52// supports the feature -- otherwise we don't define the library feature-test53// 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 < 201806L58#    error "Unexpected value of __cpp_lib_destroying_delete"59#  endif60#else61#  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#  endif64#endif65 66int 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