[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
Sat Jan 24 07:29:03 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] [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;
+}



More information about the libcxx-commits mailing list