Skip to content

Conversation

kazutakahirata
Copy link
Contributor

fwd_or_bidi_tag selects one of two implementations of
fwd_or_bidi_tag_impl depending on the condition. We can replace it
with std::conditional_t, eliminating the need for helper structs
fwd_or_bidi_tag_impl.

This patch also converts the fwd_or_bidi_tag struct into an alias
template, making "using filter_iterator" a little more readable.

fwd_or_bidi_tag selects one of two implementations of
fwd_or_bidi_tag_impl depending on the condition.  We can replace it
with std::conditional_t, eliminating the need for helper structs
fwd_or_bidi_tag_impl.

This patch also converts the fwd_or_bidi_tag struct into an alias
template, making "using filter_iterator" a little more readable.
@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

fwd_or_bidi_tag selects one of two implementations of
fwd_or_bidi_tag_impl depending on the condition. We can replace it
with std::conditional_t, eliminating the need for helper structs
fwd_or_bidi_tag_impl.

This patch also converts the fwd_or_bidi_tag struct into an alias
template, making "using filter_iterator" a little more readable.


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

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/STLExtras.h (+10-19)
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index a5c45a21a2401..5923fdd46a378 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -535,31 +535,22 @@ class filter_iterator_impl<WrappedIteratorT, PredicateT,
 
 namespace detail {
 
-template <bool is_bidirectional> struct fwd_or_bidi_tag_impl {
-  using type = std::forward_iterator_tag;
-};
-
-template <> struct fwd_or_bidi_tag_impl<true> {
-  using type = std::bidirectional_iterator_tag;
-};
-
-/// Helper which sets its type member to forward_iterator_tag if the category
-/// of \p IterT does not derive from bidirectional_iterator_tag, and to
-/// bidirectional_iterator_tag otherwise.
-template <typename IterT> struct fwd_or_bidi_tag {
-  using type = typename fwd_or_bidi_tag_impl<std::is_base_of<
-      std::bidirectional_iterator_tag,
-      typename std::iterator_traits<IterT>::iterator_category>::value>::type;
-};
+/// A type alias which is std::bidirectional_iterator_tag if the category of
+/// \p IterT derives from it, and std::forward_iterator_tag otherwise.
+template <typename IterT>
+using fwd_or_bidi_tag = std::conditional_t<
+    std::is_base_of_v<std::bidirectional_iterator_tag,
+                      typename std::iterator_traits<IterT>::iterator_category>,
+    std::bidirectional_iterator_tag, std::forward_iterator_tag>;
 
 } // namespace detail
 
 /// Defines filter_iterator to a suitable specialization of
 /// filter_iterator_impl, based on the underlying iterator's category.
 template <typename WrappedIteratorT, typename PredicateT>
-using filter_iterator = filter_iterator_impl<
-    WrappedIteratorT, PredicateT,
-    typename detail::fwd_or_bidi_tag<WrappedIteratorT>::type>;
+using filter_iterator =
+    filter_iterator_impl<WrappedIteratorT, PredicateT,
+                         detail::fwd_or_bidi_tag<WrappedIteratorT>>;
 
 /// Convenience function that takes a range of elements and a predicate,
 /// and return a new filter_iterator range.

@kazutakahirata kazutakahirata merged commit 574ff7f into llvm:main Sep 7, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250906_ADT_STLExtras_fwd_or_bidi_tag branch September 7, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants