Skip to content

Commit 7cb66a4

Browse files
authored
[libc++] Implement LWG4477: placement operator delete should be constexpr (llvm#189915)
Implement the proposed resolution of [LWG4477](https://cplusplus.github.io/LWG/issue4477). P2747R2 made placement `operator new` constexpr since C++26, but the corresponding placement `operator delete` was not. When a constructor throws during placement new in a constant expression, the placement delete is invoked and fails because it's not constexpr. Add `_LIBCPP_CONSTEXPR_SINCE_CXX26` to placement `operator delete(void*, void*)` and `operator delete[](void*, void*)`. Closes llvm#189843
1 parent 2dae0f6 commit 7cb66a4

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@
298298
"`LWG4472 <https://wg21.link/LWG4472>`__","``std::atomic_ref<const T>`` can be constructed from temporaries","2026-03 (Croydon)","","","`#189840 <https://github.com/llvm/llvm-project/issues/189840>`__",""
299299
"`LWG4474 <https://wg21.link/LWG4474>`__","""`round_to_nearest`"" rounding mode is unclear","2026-03 (Croydon)","","","`#189841 <https://github.com/llvm/llvm-project/issues/189841>`__",""
300300
"`LWG4476 <https://wg21.link/LWG4476>`__","``run_loop`` should not have a ``set_error`` completion","2026-03 (Croydon)","","","`#189842 <https://github.com/llvm/llvm-project/issues/189842>`__",""
301-
"`LWG4477 <https://wg21.link/LWG4477>`__","Placement ``operator delete`` should be constexpr","2026-03 (Croydon)","","","`#189843 <https://github.com/llvm/llvm-project/issues/189843>`__",""
301+
"`LWG4477 <https://wg21.link/LWG4477>`__","Placement ``operator delete`` should be constexpr","2026-03 (Croydon)","|Complete|","23","`#189843 <https://github.com/llvm/llvm-project/issues/189843>`__",""
302302
"`LWG4478 <https://wg21.link/LWG4478>`__","``meta::has_identifier`` is not specified for annotations","2026-03 (Croydon)","","","`#189844 <https://github.com/llvm/llvm-project/issues/189844>`__",""
303303
"`LWG4480 <https://wg21.link/LWG4480>`__","``<stdatomic.h>`` should provide ``ATOMIC_CHAR8_T_LOCK_FREE``","2026-03 (Croydon)","","","`#189845 <https://github.com/llvm/llvm-project/issues/189845>`__",""
304304
"`LWG4481 <https://wg21.link/LWG4481>`__","Disallow ``chrono::duration<const T, P>``","2026-03 (Croydon)","","","`#189846 <https://github.com/llvm/llvm-project/issues/189846>`__",""

libcxx/include/__new/placement_new_delete.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ operator new(std::size_t, void* __p) _NOEXCEPT {
2727
operator new[](std::size_t, void* __p) _NOEXCEPT {
2828
return __p;
2929
}
30-
inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}
31-
inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}
30+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void operator delete(void*, void*) _NOEXCEPT {}
31+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void operator delete[](void*, void*) _NOEXCEPT {}
3232
#endif
3333

3434
#endif // _LIBCPP___NEW_PLACEMENT_NEW_DELETE_H

libcxx/include/new

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ void operator delete[](void* ptr, std::align_val_t alignment,
8181
8282
void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20, constexpr since C++26
8383
void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20, constexpr since C++26
84-
void operator delete (void* ptr, void*) noexcept;
85-
void operator delete[](void* ptr, void*) noexcept;
84+
void operator delete (void* ptr, void*) noexcept; // constexpr since C++26
85+
void operator delete[](void* ptr, void*) noexcept; // constexpr since C++26
8686
8787
*/
8888

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.placement/new.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ TEST_CONSTEXPR_OPERATOR_NEW void test_direct_call() {
2525

2626
char ch = '*';
2727
assert(::operator new(1, &ch) == &ch);
28+
::operator delete(&ch, &ch);
2829
assert(ch == '*');
30+
31+
::operator delete(nullptr, nullptr);
2932
}
3033

3134
#ifdef __cpp_lib_constexpr_new

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.placement/new_array.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ TEST_CONSTEXPR_OPERATOR_NEW void test_direct_call() {
2626

2727
char ch = '*';
2828
assert(::operator new[](1, &ch) == &ch);
29+
::operator delete[](&ch, &ch);
2930
assert(ch == '*');
31+
32+
::operator delete[](nullptr, nullptr);
3033
}
3134

3235
#ifdef __cpp_lib_constexpr_new

0 commit comments

Comments
 (0)