Skip to content

Commit 222ece0

Browse files
committed
Add __static_fancy_pointer_cast
1 parent a318ae9 commit 222ece0

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
@@ -296,8 +296,7 @@ struct __forward_node_traits {
296296
# endif
297297

298298
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI static __begin_node_pointer __as_iter_node(__node_pointer __p) {
299-
return __p ? pointer_traits<__begin_node_pointer>::pointer_to(*static_cast<__begin_node*>(std::addressof(*__p)))
300-
: static_cast<__begin_node_pointer>(nullptr);
299+
return __static_fancy_pointer_cast<__begin_node_pointer>(__p);
301300
}
302301
};
303302

@@ -312,9 +311,7 @@ struct __forward_begin_node {
312311
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_begin_node(pointer __n) : __next_(__n) {}
313312

314313
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __next_as_begin() const {
315-
return __next_ ? pointer_traits<__begin_node_pointer>::pointer_to(
316-
*static_cast<__forward_begin_node*>(std::addressof(*__next_)))
317-
: static_cast<__begin_node_pointer>(nullptr);
314+
return __static_fancy_pointer_cast<__begin_node_pointer>(__next_);
318315
}
319316
};
320317

@@ -370,8 +367,7 @@ class _LIBCPP_TEMPLATE_VIS __forward_list_iterator {
370367

371368
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __get_begin() const { return __ptr_; }
372369
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_unsafe_node_pointer() const {
373-
return __ptr_ ? pointer_traits<__node_pointer>::pointer_to(*static_cast<__node_type*>(std::addressof(*__ptr_)))
374-
: static_cast<__node_pointer>(nullptr);
370+
return __static_fancy_pointer_cast<__node_pointer>(__ptr_);
375371
}
376372

377373
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_list_iterator(nullptr_t) _NOEXCEPT
@@ -440,8 +436,7 @@ class _LIBCPP_TEMPLATE_VIS __forward_list_const_iterator {
440436

441437
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __begin_node_pointer __get_begin() const { return __ptr_; }
442438
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_unsafe_node_pointer() const {
443-
return __ptr_ ? pointer_traits<__node_pointer>::pointer_to(*static_cast<__node_type*>(std::addressof(*__ptr_)))
444-
: static_cast<__node_pointer>(nullptr);
439+
return __static_fancy_pointer_cast<__node_pointer>(__ptr_);
445440
}
446441

447442
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI explicit __forward_list_const_iterator(nullptr_t) _NOEXCEPT

0 commit comments

Comments
 (0)