Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Simplify the implementation of the pointer aliases in allocator_traits #127079

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

philnik777
Copy link
Contributor

No description provided.

@philnik777 philnik777 force-pushed the simplify_pointer_rebind branch from 7eba3a7 to 714a015 Compare February 16, 2025 09:29
@philnik777 philnik777 marked this pull request as ready for review February 18, 2025 12:52
@philnik777 philnik777 requested a review from a team as a code owner February 18, 2025 12:52
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/127079.diff

1 Files Affected:

  • (modified) libcxx/include/__memory/allocator_traits.h (+23-35)
diff --git a/libcxx/include/__memory/allocator_traits.h b/libcxx/include/__memory/allocator_traits.h
index 2d9ab847e9f25..12229afbccddb 100644
--- a/libcxx/include/__memory/allocator_traits.h
+++ b/libcxx/include/__memory/allocator_traits.h
@@ -49,50 +49,38 @@ 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> {
+template <class _Ptr, class _Alloc, class _Tp, template <class> class _Alias, 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 = typename pointer_traits<_Ptr>::template rebind<_Tp>::other;
 #else
-  using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const _Tp>;
+  using type = typename pointer_traits<_Ptr>::template rebind<_Tp>;
 #endif
 };
 
-// __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 _Ptr, class _Alloc, class _Tp, template <class> class _Alias>
+struct __rebind_or_alias_pointer<_Ptr, _Alloc, _Tp, _Alias, __void_t<_Alias<_Alloc> > > {
+  using type = _Alias<_Alloc>;
 };
+
+template <class _Alloc>
+using __const_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_pointer;
+
+template <class _Tp, class _Ptr, class _Alloc>
+using __const_pointer _LIBCPP_NODEBUG = __rebind_or_alias_pointer<_Ptr, _Alloc, const _Tp, __const_pointer_member>;
+
+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 _LIBCPP_NODEBUG = __rebind_or_alias_pointer<_Ptr, _Alloc, void, __void_pointer_member>;
+
+template <class _Alloc>
+using __const_void_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;
 
-// __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 _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 _LIBCPP_NODEBUG =
+    __rebind_or_alias_pointer<_Ptr, _Alloc, const void, __const_void_pointer_member>;
 
 // __size_type
 template <class _Tp>

@philnik777 philnik777 force-pushed the simplify_pointer_rebind branch 3 times, most recently from 38927c3 to 82fd92b Compare March 4, 2025 09:43
@philnik777 philnik777 force-pushed the simplify_pointer_rebind branch from 82fd92b to 3408bf0 Compare March 23, 2025 20:13
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % CI

@philnik777 philnik777 force-pushed the simplify_pointer_rebind branch from 3408bf0 to 77e9721 Compare March 27, 2025 12:15
@philnik777 philnik777 force-pushed the simplify_pointer_rebind branch from 77e9721 to 43a6482 Compare March 27, 2025 13:50
@philnik777 philnik777 merged commit c13c04f into llvm:main Mar 28, 2025
75 of 78 checks passed
@philnik777 philnik777 deleted the simplify_pointer_rebind branch March 28, 2025 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants