[libcxx-commits] [libcxx] [libc++] Fix ambiguity in ranges::advance and ranges::next affecting flat_set (PR #177773)

Amgad Emara via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 25 12:08:27 PST 2026


https://github.com/TheAmgadX updated https://github.com/llvm/llvm-project/pull/177773

>From ee1360ca9f4233a7cf7c5df42d6357b6ebfeb30a Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sat, 24 Jan 2026 17:07:55 +0200
Subject: [PATCH 1/2] [libc++] Fix ambiguity in ranges::advance and
 ranges::next
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix an overload ambiguity in std::ranges::advance when the sentinel type is the
same as the iterator’s difference type.

After fixing ranges::advance, the same ambiguity was exposed in
std::ranges::next, which forwards to ranges::advance. The ambiguity is
triggered in std::flat_set initializer-list construction via the following
call chain:

  std::__inplace_merge
    -> std::__upper_bound
         -> _IterOps::next

Both ranges::advance and ranges::next are now constrained to prevent the
sentinel overload from participating when the sentinel matches
iter_difference_t<I>.

Fixes #175091.

Tests:
- flat_set initializer_list.compile.pass.cpp
---
 libcxx/include/__iterator/advance.h           |  1 +
 libcxx/include/__iterator/next.h              |  1 +
 .../initializer_list.compile.pass.cpp         | 26 +++++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp

diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index c7d3c1f0e8f05..e83cde609f5b2 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,6 +120,7 @@ struct __advance {
   // Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
   // denotes a range.
   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+    requires (!same_as<_Sp, iter_difference_t<_Ip>>)
   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
     // If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
     if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 1143ab31ff7c2..3348f5b6fb8b4 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,6 +49,7 @@ struct __next {
   }
 
   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+    requires (!same_as<_Sp, iter_difference_t<_Ip>>)
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
     ranges::advance(__x, __bound_sentinel);
     return __x;
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
new file mode 100644
index 0000000000000..194bd534d267c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <flat_set>
+
+struct T {
+  T(const auto&);
+  friend bool operator==(T, T);
+};
+
+struct Comp {
+  bool operator()(T, T) const;
+};
+
+int main(int, char**) {
+  std::flat_set<T, Comp> x = {0};
+  (void)x;
+  return 0;
+}

>From 4bd84eabf696d23bed7db2be8a766d1fa1c3cd81 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sun, 25 Jan 2026 22:07:54 +0200
Subject: [PATCH 2/2] [libc++] Implement LWG4510: Fix ambiguity in
 ranges::advance and ranges::next

---
 libcxx/docs/Status/Cxx2cIssues.csv  | 1 +
 libcxx/include/__iterator/advance.h | 2 +-
 libcxx/include/__iterator/next.h    | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 101a708b7b2de..5b023cc1043b7 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -263,4 +263,5 @@
 "`LWG4139 <https://wg21.link/LWG4139>`__","§[time.zone.leap] recursive constraint in ``<=>``","Not Adopted Yet","|Complete|","20","`#118369 <https://github.com/llvm/llvm-project/issues/118369>`__",""
 "`LWG3456 <https://wg21.link/LWG3456>`__","Pattern used by ``std::from_chars`` is underspecified (option B)","Not Adopted Yet","|Complete|","20","`#118370 <https://github.com/llvm/llvm-project/issues/118370>`__",""
 "`LWG3882 <https://wg21.link/LWG3882>`__","``tuple`` relational operators have confused friendships","Not Adopted Yet","|Complete|","22","","The comparison operators are constrained harder than the proposed resolution. libstdc++ and MSVC STL do the same."
+"`LWG4510 <https://wg21.link/LWG4510>`__","Ambiguity of ``ranges::advance`` and ``ranges::next``","Not Adopted Yet","|Complete|","","`#175091 <https://github.com/llvm/llvm-project/issues/175091>`__",""
 "","","","","","",""
diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index e83cde609f5b2..204e6012d1147 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,7 +120,7 @@ struct __advance {
   // Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
   // denotes a range.
   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
-    requires (!same_as<_Sp, iter_difference_t<_Ip>>)
+    requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
     // If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
     if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 3348f5b6fb8b4..ecd561318ad7b 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,7 +49,7 @@ struct __next {
   }
 
   template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
-    requires (!same_as<_Sp, iter_difference_t<_Ip>>)
+    requires (!__integer_like<_Sp>) && sentinel_for<_Sp, _Ip>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
     ranges::advance(__x, __bound_sentinel);
     return __x;



More information about the libcxx-commits mailing list