Skip to content

Commit 03ebbf9

Browse files
committed
Add __static_fancy_pointer_cast
1 parent bf11ff5 commit 03ebbf9

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

libcxx/include/__memory/pointer_traits.h

+10-2
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,14 @@ concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p
302302

303303
#endif
304304

305+
template <class _PtrTo, class _PtrFrom>
306+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _PtrTo __static_fancy_pointer_cast(const _PtrFrom& __p) {
307+
using __ptr_traits = pointer_traits<_PtrTo>;
308+
using __element_type = typename __ptr_traits::element_type;
309+
return __p ? __ptr_traits::pointer_to(*static_cast<__element_type*>(std::addressof(*__p)))
310+
: static_cast<_PtrTo>(nullptr);
311+
}
312+
305313
_LIBCPP_END_NAMESPACE_STD
306314

307315
_LIBCPP_POP_MACROS

libcxx/include/forward_list

+4-9
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ struct __forward_node_traits {
300300
return __p;
301301
}
302302
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI static __begin_node_pointer __as_iter_node(__node_pointer __p) {
303-
return __p ? pointer_traits<__begin_node_pointer>::pointer_to(*static_cast<__begin_node*>(std::addressof(*__p)))
304-
: static_cast<__begin_node_pointer>(nullptr);
303+
return __static_fancy_pointer_cast<__begin_node_pointer>(__p);
305304
}
306305
};
307306

@@ -316,9 +315,7 @@ struct __forward_begin_node {
316315
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_begin_node(pointer __n) : __next_(__n) {}
317316

318317
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __next_as_begin() const {
319-
return __next_ ? pointer_traits<__begin_node_pointer>::pointer_to(
320-
*static_cast<__forward_begin_node*>(std::addressof(*__next_)))
321-
: static_cast<__begin_node_pointer>(nullptr);
318+
return __static_fancy_pointer_cast<__begin_node_pointer>(__next_);
322319
}
323320
};
324321

@@ -374,8 +371,7 @@ class _LIBCPP_TEMPLATE_VIS __forward_list_iterator {
374371

375372
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __get_begin() const { return __ptr_; }
376373
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_unsafe_node_pointer() const {
377-
return __ptr_ ? pointer_traits<__node_pointer>::pointer_to(*static_cast<__node_type*>(std::addressof(*__ptr_)))
378-
: static_cast<__node_pointer>(nullptr);
374+
return __static_fancy_pointer_cast<__node_pointer>(__ptr_);
379375
}
380376

381377
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_list_iterator(nullptr_t) _NOEXCEPT
@@ -445,8 +441,7 @@ class _LIBCPP_TEMPLATE_VIS __forward_list_const_iterator {
445441

446442
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __get_begin() const { return __ptr_; }
447443
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_unsafe_node_pointer() const {
448-
return __ptr_ ? pointer_traits<__node_pointer>::pointer_to(*static_cast<__node_type*>(std::addressof(*__ptr_)))
449-
: static_cast<__node_pointer>(nullptr);
444+
return __static_fancy_pointer_cast<__node_pointer>(__ptr_);
450445
}
451446

452447
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_list_const_iterator(nullptr_t) _NOEXCEPT

0 commit comments

Comments
 (0)