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++] Use __detected_or_t to implement __has_iterator_{category,concept}_convertible_to #124456

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

philnik777
Copy link
Contributor

This simplifies the implementation a bit.

@philnik777 philnik777 force-pushed the iterator_traits_aliases branch 2 times, most recently from 5716995 to b9047ef Compare January 27, 2025 16:26
@philnik777 philnik777 force-pushed the iterator_traits_aliases branch 2 times, most recently from f676cca to 19c50ce Compare February 13, 2025 13:18
@philnik777 philnik777 force-pushed the iterator_traits_aliases branch from 19c50ce to 2c601aa Compare February 26, 2025 10:34
@philnik777 philnik777 marked this pull request as ready for review March 25, 2025 14:47
@philnik777 philnik777 requested a review from a team as a code owner March 25, 2025 14:47
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

This simplifies the implementation a bit.


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

2 Files Affected:

  • (modified) libcxx/include/__iterator/iterator_traits.h (+11-32)
  • (modified) libcxx/include/module.modulemap (+6-1)
diff --git a/libcxx/include/__iterator/iterator_traits.h b/libcxx/include/__iterator/iterator_traits.h
index db68dd2c377ac..a749d7ee52b79 100644
--- a/libcxx/include/__iterator/iterator_traits.h
+++ b/libcxx/include/__iterator/iterator_traits.h
@@ -24,6 +24,7 @@
 #include <__iterator/readable_traits.h>
 #include <__type_traits/common_reference.h>
 #include <__type_traits/conditional.h>
+#include <__type_traits/detected_or.h>
 #include <__type_traits/disjunction.h>
 #include <__type_traits/enable_if.h>
 #include <__type_traits/integral_constant.h>
@@ -32,6 +33,7 @@
 #include <__type_traits/is_primary_template.h>
 #include <__type_traits/is_reference.h>
 #include <__type_traits/is_valid_expansion.h>
+#include <__type_traits/nat.h>
 #include <__type_traits/remove_const.h>
 #include <__type_traits/remove_cv.h>
 #include <__type_traits/remove_cvref.h>
@@ -126,30 +128,6 @@ struct __has_iterator_typedefs {
   static const bool value = decltype(__test<_Tp>(nullptr, nullptr, nullptr, nullptr, nullptr))::value;
 };
 
-template <class _Tp>
-struct __has_iterator_category {
-private:
-  template <class _Up>
-  static false_type __test(...);
-  template <class _Up>
-  static true_type __test(typename _Up::iterator_category* = nullptr);
-
-public:
-  static const bool value = decltype(__test<_Tp>(nullptr))::value;
-};
-
-template <class _Tp>
-struct __has_iterator_concept {
-private:
-  template <class _Up>
-  static false_type __test(...);
-  template <class _Up>
-  static true_type __test(typename _Up::iterator_concept* = nullptr);
-
-public:
-  static const bool value = decltype(__test<_Tp>(nullptr))::value;
-};
-
 #if _LIBCPP_STD_VER >= 20
 
 // The `cpp17-*-iterator` exposition-only concepts have very similar names to the `Cpp17*Iterator` named requirements
@@ -417,18 +395,19 @@ struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*> {
 #endif
 };
 
-template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
-struct __has_iterator_category_convertible_to : is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up> {
-};
+template <class _Tp>
+using __iterator_category _LIBCPP_NODEBUG = typename _Tp::iterator_category;
 
-template <class _Tp, class _Up>
-struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
+template <class _Tp>
+using __iterator_concept _LIBCPP_NODEBUG = typename _Tp::iterator_concept;
 
-template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
-struct __has_iterator_concept_convertible_to : is_convertible<typename _Tp::iterator_concept, _Up> {};
+template <class _Tp, class _Up>
+using __has_iterator_category_convertible_to _LIBCPP_NODEBUG =
+    is_convertible<__detected_or_t<__nat, __iterator_category, iterator_traits<_Tp> >, _Up>;
 
 template <class _Tp, class _Up>
-struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
+using __has_iterator_concept_convertible_to _LIBCPP_NODEBUG =
+    is_convertible<__detected_or_t<__nat, __iterator_concept, _Tp>, _Up>;
 
 template <class _Tp>
 using __has_input_iterator_category _LIBCPP_NODEBUG = __has_iterator_category_convertible_to<_Tp, input_iterator_tag>;
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 0f2016e7f5d15..c44807cc6884c 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -410,7 +410,11 @@ module std [system] {
     module copy_backward                          { header "__algorithm/copy_backward.h" }
     module copy_if                                { header "__algorithm/copy_if.h" }
     module copy_move_common                       { header "__algorithm/copy_move_common.h" }
-    module copy_n                                 { header "__algorithm/copy_n.h" }
+    module copy_n                                 {
+      header "__algorithm/copy_n.h"
+
+      export std.iterator_traits
+    }
     module copy                                   { header "__algorithm/copy.h" }
     module count_if                               { header "__algorithm/count_if.h" }
     module count                                  { header "__algorithm/count.h" }
@@ -1456,6 +1460,7 @@ module std [system] {
     module iterator_traits {
       header "__iterator/iterator_traits.h"
       export std_core.type_traits.integral_constant
+      export std_core.type_traits.is_convertible
     }
     module iterator_with_data         { header "__iterator/iterator_with_data.h" }
     module iterator                   { header "__iterator/iterator.h" }

module copy_n {
header "__algorithm/copy_n.h"

export std.iterator_traits
Copy link
Member

Choose a reason for hiding this comment

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

Why is that export necessary?

Copy link
Contributor Author

@philnik777 philnik777 Mar 26, 2025

Choose a reason for hiding this comment

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

I can't remember anymore why, but I'm quite certain I've looked into this and it made some amount of sense.

@philnik777 philnik777 force-pushed the iterator_traits_aliases branch from 2c601aa to 9af8f55 Compare March 26, 2025 07:55
@philnik777 philnik777 merged commit 3b2b918 into llvm:main Mar 26, 2025
11 of 16 checks passed
@philnik777 philnik777 deleted the iterator_traits_aliases branch March 26, 2025 07:55
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