[libcxx-commits] [libcxx] [libc++] Fix -Wsign-compare warning in `ranges::search` (PR #100983)

Hewill Kang via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jul 29 01:23:34 PDT 2024


https://github.com/hewillk created https://github.com/llvm/llvm-project/pull/100983

The type returned by `ranges::size` is not guaranteed to be unsigned, so using `ranges::distance` here ensures that it always returns a signed type.

>From cfdba93f2861ac8f6026fc59eb1f3ff455cfd3d4 Mon Sep 17 00:00:00 2001
From: Hewill Kang <hewillk at gmail.com>
Date: Mon, 29 Jul 2024 16:21:22 +0800
Subject: [PATCH] [libc++]

---
 libcxx/include/__algorithm/ranges_search.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__algorithm/ranges_search.h b/libcxx/include/__algorithm/ranges_search.h
index 55294c60631b1..0f1cc5cd3095b 100644
--- a/libcxx/include/__algorithm/ranges_search.h
+++ b/libcxx/include/__algorithm/ranges_search.h
@@ -98,11 +98,11 @@ struct __fn {
       _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
     auto __first1 = ranges::begin(__range1);
     if constexpr (sized_range<_Range2>) {
-      auto __size2 = ranges::size(__range2);
+      auto __size2 = ranges::distance(__range2);
       if (__size2 == 0)
         return {__first1, __first1};
       if constexpr (sized_range<_Range1>) {
-        auto __size1 = ranges::size(__range1);
+        auto __size1 = ranges::distance(__range1);
         if (__size1 < __size2) {
           ranges::advance(__first1, ranges::end(__range1));
           return {__first1, __first1};



More information about the libcxx-commits mailing list