Skip to content

Commit 38fb011

Browse files
authored
[libc++] Make forward_list constexpr as part of P3372R3 (#129435)
Fixes #128658
1 parent 2ab83e9 commit 38fb011

File tree

76 files changed

+1186
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1186
-459
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ Status
420420
---------------------------------------------------------- -----------------
421421
``__cpp_lib_constexpr_algorithms`` ``202306L``
422422
---------------------------------------------------------- -----------------
423+
``__cpp_lib_constexpr_forward_list`` ``202502L``
424+
---------------------------------------------------------- -----------------
423425
``__cpp_lib_constexpr_new`` ``202406L``
424426
---------------------------------------------------------- -----------------
425427
``__cpp_lib_constexpr_queue`` ``202502L``

libcxx/include/__memory/allocation_guard.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,26 @@ struct __allocation_guard {
4949
using _Size _LIBCPP_NODEBUG = typename allocator_traits<_Alloc>::size_type;
5050

5151
template <class _AllocT> // we perform the allocator conversion inside the constructor
52-
_LIBCPP_HIDE_FROM_ABI explicit __allocation_guard(_AllocT __alloc, _Size __n)
52+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __allocation_guard(_AllocT __alloc, _Size __n)
5353
: __alloc_(std::move(__alloc)),
5454
__n_(__n),
5555
__ptr_(allocator_traits<_Alloc>::allocate(__alloc_, __n_)) // initialization order is important
5656
{}
5757

58-
_LIBCPP_HIDE_FROM_ABI ~__allocation_guard() _NOEXCEPT { __destroy(); }
58+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI ~__allocation_guard() _NOEXCEPT { __destroy(); }
5959

60-
_LIBCPP_HIDE_FROM_ABI __allocation_guard(const __allocation_guard&) = delete;
61-
_LIBCPP_HIDE_FROM_ABI __allocation_guard(__allocation_guard&& __other) _NOEXCEPT
60+
__allocation_guard(const __allocation_guard&) = delete;
61+
__allocation_guard& operator=(const __allocation_guard& __other) = delete;
62+
63+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __allocation_guard(__allocation_guard&& __other) _NOEXCEPT
6264
: __alloc_(std::move(__other.__alloc_)),
6365
__n_(__other.__n_),
6466
__ptr_(__other.__ptr_) {
6567
__other.__ptr_ = nullptr;
6668
}
6769

68-
_LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(const __allocation_guard& __other) = delete;
69-
_LIBCPP_HIDE_FROM_ABI __allocation_guard& operator=(__allocation_guard&& __other) _NOEXCEPT {
70+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __allocation_guard&
71+
operator=(__allocation_guard&& __other) _NOEXCEPT {
7072
if (std::addressof(__other) != this) {
7173
__destroy();
7274

@@ -79,17 +81,17 @@ struct __allocation_guard {
7981
return *this;
8082
}
8183

82-
_LIBCPP_HIDE_FROM_ABI _Pointer
84+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI _Pointer
8385
__release_ptr() _NOEXCEPT { // not called __release() because it's a keyword in objective-c++
8486
_Pointer __tmp = __ptr_;
8587
__ptr_ = nullptr;
8688
return __tmp;
8789
}
8890

89-
_LIBCPP_HIDE_FROM_ABI _Pointer __get() const _NOEXCEPT { return __ptr_; }
91+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI _Pointer __get() const _NOEXCEPT { return __ptr_; }
9092

9193
private:
92-
_LIBCPP_HIDE_FROM_ABI void __destroy() _NOEXCEPT {
94+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void __destroy() _NOEXCEPT {
9395
if (__ptr_ != nullptr) {
9496
allocator_traits<_Alloc>::deallocate(__alloc_, __ptr_, __n_);
9597
}

libcxx/include/__memory/pointer_traits.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {
245245
}
246246

247247
template <class _Pointer>
248-
inline _LIBCPP_HIDE_FROM_ABI constexpr auto
249-
to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {
248+
inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(const _Pointer& __p) noexcept
249+
-> decltype(std::__to_address(__p)) {
250250
return std::__to_address(__p);
251251
}
252252
#endif
@@ -302,6 +302,18 @@ concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p
302302

303303
#endif
304304

305+
// This function ensures safe conversions between fancy pointers at compile-time, where we avoid casts from/to
306+
// `__void_pointer` by obtaining the underlying raw pointer from the fancy pointer using `std::to_address`,
307+
// then dereferencing it to retrieve the pointed-to object, and finally constructing the target fancy pointer
308+
// to that object using the `std::pointer_traits<>::pinter_to` function.
309+
template <class _PtrTo, class _PtrFrom>
310+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _PtrTo __static_fancy_pointer_cast(const _PtrFrom& __p) {
311+
using __ptr_traits = pointer_traits<_PtrTo>;
312+
using __element_type = typename __ptr_traits::element_type;
313+
return __p ? __ptr_traits::pointer_to(*static_cast<__element_type*>(std::addressof(*__p)))
314+
: static_cast<_PtrTo>(nullptr);
315+
}
316+
305317
_LIBCPP_END_NAMESPACE_STD
306318

307319
_LIBCPP_POP_MACROS

0 commit comments

Comments
 (0)