[libcxx-commits] [libcxx] 3a653af - [libc++] Simplify the implementation of __{un, re}wrap_range (#178381)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 4 01:31:35 PST 2026


Author: Nikolas Klauser
Date: 2026-02-04T10:31:31+01:00
New Revision: 3a653afd45709432181952c0ffdb53eceb0939ae

URL: https://github.com/llvm/llvm-project/commit/3a653afd45709432181952c0ffdb53eceb0939ae
DIFF: https://github.com/llvm/llvm-project/commit/3a653afd45709432181952c0ffdb53eceb0939ae.diff

LOG: [libc++] Simplify the implementation of __{un,re}wrap_range (#178381)

We can use a relatively simple `if constexpr` chain instead of SFINAE
and class template specialization, making the functions much simpler to
understand.

Added: 
    

Modified: 
    libcxx/include/__algorithm/unwrap_range.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__algorithm/unwrap_range.h b/libcxx/include/__algorithm/unwrap_range.h
index 2d4b9bb5545ad..14603a4026381 100644
--- a/libcxx/include/__algorithm/unwrap_range.h
+++ b/libcxx/include/__algorithm/unwrap_range.h
@@ -10,11 +10,9 @@
 #define _LIBCPP___ALGORITHM_UNWRAP_RANGE_H
 
 #include <__algorithm/unwrap_iter.h>
-#include <__concepts/constructible.h>
 #include <__config>
 #include <__iterator/concepts.h>
-#include <__iterator/next.h>
-#include <__utility/declval.h>
+#include <__type_traits/is_same.h>
 #include <__utility/move.h>
 #include <__utility/pair.h>
 
@@ -33,52 +31,24 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 template <class _Iter, class _Sent>
-struct __unwrap_range_impl {
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Sent __sent)
-    requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
-  {
-    auto __last = ranges::next(__first, __sent);
+_LIBCPP_HIDE_FROM_ABI constexpr auto __unwrap_range(_Iter __first, _Sent __last) {
+  if constexpr (is_same_v<_Iter, _Sent>)
     return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last))};
-  }
-
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Sent __last) {
+  else if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>) {
+    auto __iter_last = __first + (__last - __first);
+    return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__iter_last))};
+  } else
     return pair{std::move(__first), std::move(__last)};
-  }
-
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto
-  __rewrap(_Iter __orig_iter, decltype(std::__unwrap_iter(std::move(__orig_iter))) __iter)
-    requires random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>
-  {
-    return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
-  }
-
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto __rewrap(const _Iter&, _Iter __iter)
-    requires(!(random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>))
-  {
-    return __iter;
-  }
-};
-
-template <class _Iter>
-struct __unwrap_range_impl<_Iter, _Iter> {
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto __unwrap(_Iter __first, _Iter __last) {
-    return pair{std::__unwrap_iter(std::move(__first)), std::__unwrap_iter(std::move(__last))};
-  }
-
-  _LIBCPP_HIDE_FROM_ABI static constexpr auto
-  __rewrap(_Iter __orig_iter, decltype(std::__unwrap_iter(__orig_iter)) __iter) {
-    return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
-  }
-};
-
-template <class _Iter, class _Sent>
-_LIBCPP_HIDE_FROM_ABI constexpr auto __unwrap_range(_Iter __first, _Sent __last) {
-  return __unwrap_range_impl<_Iter, _Sent>::__unwrap(std::move(__first), std::move(__last));
 }
 
 template < class _Sent, class _Iter, class _Unwrapped>
 _LIBCPP_HIDE_FROM_ABI constexpr _Iter __rewrap_range(_Iter __orig_iter, _Unwrapped __iter) {
-  return __unwrap_range_impl<_Iter, _Sent>::__rewrap(std::move(__orig_iter), std::move(__iter));
+  if constexpr (is_same_v<_Iter, _Sent>)
+    return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
+  else if constexpr (random_access_iterator<_Iter> && sized_sentinel_for<_Sent, _Iter>)
+    return std::__rewrap_iter(std::move(__orig_iter), std::move(__iter));
+  else
+    return __iter;
 }
 #else  // _LIBCPP_STD_VER >= 20
 template <class _Iter, class _Unwrapped = decltype(std::__unwrap_iter(std::declval<_Iter>()))>


        


More information about the libcxx-commits mailing list