[libcxx-commits] [libcxx] c13c04f - [libc++] Simplify the implementation of the pointer aliases in allocator_traits (#127079)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Mar 28 02:27:03 PDT 2025
Author: Nikolas Klauser
Date: 2025-03-28T10:27:00+01:00
New Revision: c13c04fdfe9b312160310eeeee785867627ffe2f
URL: https://github.com/llvm/llvm-project/commit/c13c04fdfe9b312160310eeeee785867627ffe2f
DIFF: https://github.com/llvm/llvm-project/commit/c13c04fdfe9b312160310eeeee785867627ffe2f.diff
LOG: [libc++] Simplify the implementation of the pointer aliases in allocator_traits (#127079)
Added:
Modified:
libcxx/include/__memory/allocator_traits.h
Removed:
################################################################################
diff --git a/libcxx/include/__memory/allocator_traits.h b/libcxx/include/__memory/allocator_traits.h
index 7e964b7647014..65ea352da7992 100644
--- a/libcxx/include/__memory/allocator_traits.h
+++ b/libcxx/include/__memory/allocator_traits.h
@@ -36,12 +36,6 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
-#define _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(NAME, PROPERTY) \
- template <class _Tp, class = void> \
- struct NAME : false_type {}; \
- template <class _Tp> \
- struct NAME<_Tp, __void_t<typename _Tp::PROPERTY > > : true_type {}
-
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
// __pointer
template <class _Tp>
@@ -50,51 +44,45 @@ using __pointer_member _LIBCPP_NODEBUG = typename _Tp::pointer;
template <class _Tp, class _Alloc>
using __pointer _LIBCPP_NODEBUG = __detected_or_t<_Tp*, __pointer_member, __libcpp_remove_reference_t<_Alloc> >;
-// __const_pointer
-_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_pointer, const_pointer);
-template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
-struct __const_pointer {
- using type _LIBCPP_NODEBUG = typename _Alloc::const_pointer;
-};
-template <class _Tp, class _Ptr, class _Alloc>
-struct __const_pointer<_Tp, _Ptr, _Alloc, false> {
+// This trait returns _Alias<_Alloc> if that's well-formed, and _Ptr rebound to _Tp otherwise
+template <class _Alloc, template <class> class _Alias, class _Ptr, class _Tp, class = void>
+struct __rebind_or_alias_pointer {
#ifdef _LIBCPP_CXX03_LANG
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const _Tp>::other;
+ using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<_Tp>::other;
#else
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const _Tp>;
+ using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<_Tp>;
#endif
};
+
+template <class _Ptr, class _Alloc, class _Tp, template <class> class _Alias>
+struct __rebind_or_alias_pointer<_Alloc, _Alias, _Ptr, _Tp, __void_t<_Alias<_Alloc> > > {
+ using type _LIBCPP_NODEBUG = _Alias<_Alloc>;
+};
+
+// __const_pointer
+template <class _Alloc>
+using __const_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_pointer;
+
+template <class _Tp, class _Ptr, class _Alloc>
+using __const_pointer_t _LIBCPP_NODEBUG =
+ typename __rebind_or_alias_pointer<_Alloc, __const_pointer_member, _Ptr, const _Tp>::type;
_LIBCPP_SUPPRESS_DEPRECATED_POP
// __void_pointer
-_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_void_pointer, void_pointer);
-template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
-struct __void_pointer {
- using type _LIBCPP_NODEBUG = typename _Alloc::void_pointer;
-};
+template <class _Alloc>
+using __void_pointer_member _LIBCPP_NODEBUG = typename _Alloc::void_pointer;
+
template <class _Ptr, class _Alloc>
-struct __void_pointer<_Ptr, _Alloc, false> {
-#ifdef _LIBCPP_CXX03_LANG
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>::other;
-#else
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>;
-#endif
-};
+using __void_pointer_t _LIBCPP_NODEBUG =
+ typename __rebind_or_alias_pointer<_Alloc, __void_pointer_member, _Ptr, void>::type;
// __const_void_pointer
-_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_void_pointer, const_void_pointer);
-template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
-struct __const_void_pointer {
- using type _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;
-};
+template <class _Alloc>
+using __const_void_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;
+
template <class _Ptr, class _Alloc>
-struct __const_void_pointer<_Ptr, _Alloc, false> {
-#ifdef _LIBCPP_CXX03_LANG
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>::other;
-#else
- using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>;
-#endif
-};
+using __const_void_pointer_t _LIBCPP_NODEBUG =
+ typename __rebind_or_alias_pointer<_Alloc, __const_void_pointer_member, _Ptr, const void>::type;
// __size_type
template <class _Tp>
@@ -104,13 +92,13 @@ template <class _Alloc, class _DiffType>
using __size_type _LIBCPP_NODEBUG = __detected_or_t<__make_unsigned_t<_DiffType>, __size_type_member, _Alloc>;
// __alloc_traits_
diff erence_type
-_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_alloc_traits_
diff erence_type,
diff erence_type);
-template <class _Alloc, class _Ptr, bool = __has_alloc_traits_
diff erence_type<_Alloc>::value>
+template <class _Alloc, class _Ptr, class = void>
struct __alloc_traits_
diff erence_type {
using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::
diff erence_type;
};
+
template <class _Alloc, class _Ptr>
-struct __alloc_traits_
diff erence_type<_Alloc, _Ptr, true> {
+struct __alloc_traits_
diff erence_type<_Alloc, _Ptr, __void_t<typename _Alloc::
diff erence_type> > {
using type _LIBCPP_NODEBUG = typename _Alloc::
diff erence_type;
};
@@ -240,9 +228,9 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
using allocator_type = _Alloc;
using value_type = typename allocator_type::value_type;
using pointer = __pointer<value_type, allocator_type>;
- using const_pointer = typename __const_pointer<value_type, pointer, allocator_type>::type;
- using void_pointer = typename __void_pointer<pointer, allocator_type>::type;
- using const_void_pointer = typename __const_void_pointer<pointer, allocator_type>::type;
+ using const_pointer = __const_pointer_t<value_type, pointer, allocator_type>;
+ using void_pointer = __void_pointer_t<pointer, allocator_type>;
+ using const_void_pointer = __const_void_pointer_t<pointer, allocator_type>;
using
diff erence_type = typename __alloc_traits_
diff erence_type<allocator_type, pointer>::type;
using size_type = __size_type<allocator_type,
diff erence_type>;
using propagate_on_container_copy_assignment = __propagate_on_container_copy_assignment<allocator_type>;
@@ -386,8 +374,6 @@ inline const bool __is_cpp17_copy_insertable_v =
(!__is_std_allocator_v<_Alloc> &&
__has_construct_v<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>));
-#undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX
-
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
More information about the libcxx-commits
mailing list