[libcxx-commits] [libcxx] 6b4dfcf - [libc++][NFC] Update implementation strategy of the `operator()(I&& first, S last)` for `std::ranges::distance` (#215994)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 13 05:12:32 PDT 2026


Author: Lucas Mellone
Date: 2026-08-13T20:12:27+08:00
New Revision: 6b4dfcfc860bd348fc3e42350e9d411abbc44e1b

URL: https://github.com/llvm/llvm-project/commit/6b4dfcfc860bd348fc3e42350e9d411abbc44e1b
DIFF: https://github.com/llvm/llvm-project/commit/6b4dfcfc860bd348fc3e42350e9d411abbc44e1b.diff

LOG: [libc++][NFC] Update implementation strategy of the `operator()(I&& first, S last)` for `std::ranges::distance` (#215994)

This patch updates the implementation strategy for `ranges::distance(I&&
first, S last)` to match the exact wording from the Standard.

This implementation is NFC because the return type and the branching
condition will be the same after the change, and the tests present in
`libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.distance/iterator_sentinel.pass.cpp`
are already covering the scenarios for such change.

References:
- https://cplusplus.github.io/LWG/issue3664
- https://cplusplus.github.io/LWG/issue4242
- https://wg21.link/range.iter.op.distance

Follows up: https://github.com/llvm/llvm-project/pull/211568

Added: 
    

Modified: 
    libcxx/include/__iterator/distance.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__iterator/distance.h b/libcxx/include/__iterator/distance.h
index c7c4ff22d6e9f..1d1b37b7b662d 100644
--- a/libcxx/include/__iterator/distance.h
+++ b/libcxx/include/__iterator/distance.h
@@ -22,7 +22,8 @@
 #include <__ranges/size.h>
 #include <__type_traits/decay.h>
 #include <__type_traits/enable_if.h>
-#include <__type_traits/remove_cvref.h>
+#include <__type_traits/is_array.h>
+#include <__type_traits/remove_reference.h>
 #include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -86,11 +87,12 @@ struct __distance {
   }
 
   template <class _Ip, sized_sentinel_for<decay_t<_Ip>> _Sp>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iter_
diff erence_t<_Ip> operator()(_Ip&& __first, _Sp __last) const {
-    if constexpr (sized_sentinel_for<_Sp, __remove_cvref_t<_Ip>>) {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iter_
diff erence_t<decay_t<_Ip>>
+  operator()(_Ip&& __first, _Sp __last) const {
+    if constexpr (!is_array_v<remove_reference_t<_Ip>>) {
       return __last - __first;
     } else {
-      return __last - decay_t<_Ip>(__first);
+      return __last - static_cast<decay_t<_Ip>>(__first);
     }
   }
 


        


More information about the libcxx-commits mailing list