67 lines · cpp
1// RUN: %clang_cc1 -verify=expected,both -std=c++26 %s -fexperimental-new-constant-interpreter2// RUN: %clang_cc1 -verify=ref,both -std=c++26 %s3 4// both-no-diagnostics5 6namespace std {7 struct type_info;8 struct destroying_delete_t {9 explicit destroying_delete_t() = default;10 } inline constexpr destroying_delete{};11 struct nothrow_t {12 explicit nothrow_t() = default;13 } inline constexpr nothrow{};14 using size_t = decltype(sizeof(0));15 enum class align_val_t : size_t {};16};17 18constexpr void *operator new(std::size_t, void *p) { return p; }19namespace std {20 template<typename T> constexpr T *construct_at(T *p) { return new (p) T; }21 template<typename T> constexpr void destroy_at(T *p) { p->~T(); }22}23 24constexpr bool foo() {25 using T = bool;26 bool b = true;27 b.~T();28 new (&b) bool(false);29 return b;30}31static_assert(!foo());32 33struct S {};34constexpr bool foo2() {35 S s;36 s.~S();37 new (&s) S{};38 return true;39}40static_assert(foo2());41 42constexpr void destroy_pointer() {43 using T = int*;44 T p;45 p.~T();46 std::construct_at(&p);47}48static_assert((destroy_pointer(), true));49 50 51namespace DestroyArrayElem {52 /// This is proof that std::destroy_at'ing an array element53 /// ends the lifetime of the entire array.54 /// See https://github.com/llvm/llvm-project/issues/14752855 /// Using destroy_at on array elements is currently a no-op due to this.56 constexpr int test() {57 int a[4] = {};58 59 std::destroy_at(&a[3]);60 int r = a[1];61 std::construct_at(&a[3]);62 63 return r;64 }65 static_assert(test() == 0);66}67