[libcxx-commits] [libcxx] d5d7c9c - [libc++] Implement `ranges::fold_right_last` (completes P2322R6) (#195580)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon May 11 03:35:20 PDT 2026


Author: Connector Switch
Date: 2026-05-11T18:35:15+08:00
New Revision: d5d7c9c59fae10c459885b81f83ee73eaf0ba36d

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

LOG: [libc++] Implement `ranges::fold_right_last` (completes P2322R6) (#195580)

- Closes https://github.com/llvm/llvm-project/issues/105208.
- Closes https://github.com/llvm/llvm-project/issues/174061.
- Closes https://github.com/llvm/llvm-project/issues/174063.

Added: 
    libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/ranges.fold_right_last.pass.cpp

Modified: 
    libcxx/docs/FeatureTestMacroTable.rst
    libcxx/docs/ReleaseNotes/23.rst
    libcxx/docs/Status/Cxx23Papers.csv
    libcxx/include/__algorithm/ranges_fold.h
    libcxx/include/algorithm
    libcxx/include/version
    libcxx/modules/std/algorithm.inc
    libcxx/test/benchmarks/algorithms/nonmodifying/fold.bench.cpp
    libcxx/test/libcxx/algorithms/nodiscard.verify.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
    libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 941c1c23c7d8a..ae48eaed1f46b 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -380,6 +380,8 @@ Status
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_find_last``                             ``202207L``
     ---------------------------------------------------------- -----------------
+    ``__cpp_lib_ranges_fold``                                  ``202207L``
+    ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_iota``                                  ``202202L``
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_join_with``                             ``202202L``

diff  --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index a34e379b145fe..a55869a8bf783 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -46,8 +46,7 @@ Implemented Papers
 - P2781R9: ``std::constant_wrapper`` (`Github <https://llvm.org/PR148179>`__)
 - P3978R3: ``constant_wrapper`` should unwrap on call and subscript (`Github <https://llvm.org/PR189605>`__)
 - P2164R9: ``views::enumerate`` (`Github <https://llvm.org/PR105251>`__)
-- P2322R6 (partial): ``ranges::fold_left_first``, ``ranges::fold_left_first_with_iter``, ``ranges::fold_right`` are
-  supported (`Github <https://llvm.org/PR105208>`__)
+- P2322R6: ``ranges::fold`` (`Github <https://llvm.org/PR105208>`__)
 - P4144R1: Remove ``span``'s ``initializer_list`` constructor for C++26 (`Github <https://llvm.org/PR189612>`__)
 
 Improvements and New Features

diff  --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 0fff5258729f7..eb580ea891f5b 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -65,7 +65,7 @@
 "`P2286R8 <https://wg21.link/P2286R8>`__","Formatting Ranges","2022-07 (Virtual)","|Complete|","16","`#105202 <https://github.com/llvm/llvm-project/issues/105202>`__",""
 "`P2291R3 <https://wg21.link/P2291R3>`__","Add Constexpr Modifiers to Functions ``to_chars`` and ``from_chars`` for Integral Types in ``<charconv>`` Header","2022-07 (Virtual)","|Complete|","16","`#105204 <https://github.com/llvm/llvm-project/issues/105204>`__",""
 "`P2302R4 <https://wg21.link/P2302R4>`__","``std::ranges::contains``","2022-07 (Virtual)","|Complete|","19","`#105206 <https://github.com/llvm/llvm-project/issues/105206>`__",""
-"`P2322R6 <https://wg21.link/P2322R6>`__","``ranges::fold``","2022-07 (Virtual)","|Partial|","","Only ``fold_left_with_iter``, ``fold_left``, ``fold_left_first_with_iter``, ``fold_left_first`` and ``fold_right`` are implemented."
+"`P2322R6 <https://wg21.link/P2322R6>`__","``ranges::fold``","2022-07 (Virtual)","|Complete|","23","`#105208 <https://github.com/llvm/llvm-project/issues/105208>`__",""
 "`P2374R4 <https://wg21.link/P2374R4>`__","``views::cartesian_product``","2022-07 (Virtual)","","","`#105209 <https://github.com/llvm/llvm-project/issues/105209>`__",""
 "`P2404R3 <https://wg21.link/P2404R3>`__","Move-only types for ``equality_comparable_with``, ``totally_ordered_with``, and ``three_way_comparable_with``","2022-07 (Virtual)","|Complete|","22","`#105210 <https://github.com/llvm/llvm-project/issues/105210>`__","Implemented as a DR in C++20."
 "`P2408R5 <https://wg21.link/P2408R5>`__","Ranges iterators as inputs to non-Ranges algorithms","2022-07 (Virtual)","","","`#105211 <https://github.com/llvm/llvm-project/issues/105211>`__",""

diff  --git a/libcxx/include/__algorithm/ranges_fold.h b/libcxx/include/__algorithm/ranges_fold.h
index 2e36a7ce3a86c..87579b264296a 100644
--- a/libcxx/include/__algorithm/ranges_fold.h
+++ b/libcxx/include/__algorithm/ranges_fold.h
@@ -23,6 +23,7 @@
 #include <__iterator/concepts.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/next.h>
+#include <__iterator/prev.h>
 #include <__iterator/reverse_iterator.h>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
@@ -234,6 +235,30 @@ struct __fold_right {
 };
 
 inline constexpr auto fold_right = __fold_right();
+
+struct __fold_right_last {
+  template <bidirectional_iterator _Iter,
+            sentinel_for<_Iter> _Sp,
+            __indirectly_binary_right_foldable<iter_value_t<_Iter>, _Iter> _Func>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Iter __first, _Sp __last, _Func __func) {
+    using _Up = decltype(fold_right(__first, __last, iter_value_t<_Iter>(*__first), __func));
+
+    if (__first == __last)
+      return optional<_Up>();
+
+    _Iter __tail = ranges::prev(ranges::next(__first, __last));
+    return optional<_Up>(
+        in_place, ranges::fold_right(std::move(__first), __tail, iter_value_t<_Iter>(*__tail), std::move(__func)));
+  }
+
+  template <bidirectional_range _Range,
+            __indirectly_binary_right_foldable<range_value_t<_Range>, iterator_t<_Range>> _Func>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Range&& __range, _Func __func) {
+    return operator()(ranges::begin(__range), ranges::end(__range), std::ref(__func));
+  }
+};
+
+inline constexpr auto fold_right_last = __fold_right_last();
 } // namespace ranges
 
 #endif // _LIBCPP_STD_VER >= 23

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index d332b8ce4fd70..95446b44eaf9c 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -962,11 +962,21 @@ namespace ranges {
 
   template<bidirectional_iterator I, sentinel_for<I> S, class T = iter_value_t<I>,
           indirectly-binary-right-foldable<T, I> F>
-    constexpr auto ranges::fold_right(I first, S last, T init, F f);
+    constexpr auto ranges::fold_right(I first, S last, T init, F f);                                // since C++23
 
   template<bidirectional_range R, class T = range_value_t<R>,
           indirectly-binary-right-foldable<T, iterator_t<R>> F>
-    constexpr auto ranges::fold_right(R&& r, T init, F f);
+    constexpr auto ranges::fold_right(R&& r, T init, F f);                                          // since C++23
+
+  template<bidirectional_iterator I, sentinel_for<I> S,
+          indirectly-binary-right-foldable<iter_value_t<I>, I> F>
+    requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+    constexpr auto ranges::fold_right_last(I first, S last, F f);                                   // since C++23
+
+  template<bidirectional_range R,
+          indirectly-binary-right-foldable<range_value_t<R>, iterator_t<R>> F>
+    requires constructible_from<range_value_t<R>, range_reference_t<R>>
+    constexpr auto ranges::fold_right_last(R&& r, F f);                                             // since C++23
 
   template<class I, class T>
     using fold_left_with_iter_result = in_value_result<I, T>;                                       // since C++23

diff  --git a/libcxx/include/version b/libcxx/include/version
index 8ff06dae7bb0a..1c683b67e5700 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -212,6 +212,7 @@ __cpp_lib_ranges_concat                                 202403L <ranges>
 __cpp_lib_ranges_contains                               202207L <algorithm>
 __cpp_lib_ranges_enumerate                              202302L <ranges>
 __cpp_lib_ranges_find_last                              202207L <algorithm>
+__cpp_lib_ranges_fold                                   202207L <algorithm>
 __cpp_lib_ranges_indices                                202506L <ranges>
 __cpp_lib_ranges_iota                                   202202L <numeric>
 __cpp_lib_ranges_join_with                              202202L <ranges>
@@ -529,6 +530,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 # define __cpp_lib_ranges_contains                      202207L
 # define __cpp_lib_ranges_enumerate                     202302L
 # define __cpp_lib_ranges_find_last                     202207L
+# define __cpp_lib_ranges_fold                          202207L
 # define __cpp_lib_ranges_iota                          202202L
 # define __cpp_lib_ranges_join_with                     202202L
 # define __cpp_lib_ranges_repeat                        202207L

diff  --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 3a59726135fe7..1db72eff08757 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -170,9 +170,7 @@ export namespace std {
     using std::ranges::fold_left_with_iter;
     using std::ranges::fold_left_with_iter_result;
     using std::ranges::fold_right;
-#  if 0
     using std::ranges::fold_right_last;
-#  endif
 #endif // _LIBCPP_STD_VER >= 23
   }    // namespace ranges
 

diff  --git a/libcxx/test/benchmarks/algorithms/nonmodifying/fold.bench.cpp b/libcxx/test/benchmarks/algorithms/nonmodifying/fold.bench.cpp
index 1e061cee3a07a..95d71fad21a34 100644
--- a/libcxx/test/benchmarks/algorithms/nonmodifying/fold.bench.cpp
+++ b/libcxx/test/benchmarks/algorithms/nonmodifying/fold.bench.cpp
@@ -28,6 +28,12 @@ int main(int argc, char** argv) {
   auto std_ranges_fold_left_first = [](auto first, auto last, auto, auto func) {
     return std::ranges::fold_left_first(first, last, func);
   };
+  auto std_ranges_fold_right = [](auto first, auto last, auto init, auto func) {
+    return std::ranges::fold_right(first, last, init, func);
+  };
+  auto std_ranges_fold_right_last = [](auto first, auto last, auto, auto func) {
+    return std::ranges::fold_right_last(first, last, func);
+  };
   // ranges::{fold_left,fold_left_first,fold_right,fold_right_last}
   {
     auto bm = []<class Container>(std::string name, auto fold) {
@@ -71,9 +77,13 @@ int main(int argc, char** argv) {
     bm.operator()<std::deque<unsigned int>>("rng::fold_left_first(deque<int>)", std_ranges_fold_left_first);
     bm.operator()<std::list<unsigned int>>("rng::fold_left_first(list<int>)", std_ranges_fold_left_first);
 
-    bm.operator()<std::vector<unsigned int>>("rng::fold_right(vector<int>)", std::ranges::fold_right);
-    bm.operator()<std::deque<unsigned int>>("rng::fold_right(deque<int>)", std::ranges::fold_right);
-    bm.operator()<std::list<unsigned int>>("rng::fold_right(list<int>)", std::ranges::fold_right);
+    bm.operator()<std::vector<unsigned int>>("rng::fold_right(vector<int>)", std_ranges_fold_right);
+    bm.operator()<std::deque<unsigned int>>("rng::fold_right(deque<int>)", std_ranges_fold_right);
+    bm.operator()<std::list<unsigned int>>("rng::fold_right(list<int>)", std_ranges_fold_right);
+
+    bm.operator()<std::vector<unsigned int>>("rng::fold_right_last(vector<int>)", std_ranges_fold_right_last);
+    bm.operator()<std::deque<unsigned int>>("rng::fold_right_last(deque<int>)", std_ranges_fold_right_last);
+    bm.operator()<std::list<unsigned int>>("rng::fold_right_last(list<int>)", std_ranges_fold_right_last);
   }
 
   benchmark::Initialize(&argc, argv);

diff  --git a/libcxx/test/libcxx/algorithms/nodiscard.verify.cpp b/libcxx/test/libcxx/algorithms/nodiscard.verify.cpp
index 4530d8c9507f6..2bb7f59b24dcd 100644
--- a/libcxx/test/libcxx/algorithms/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/nodiscard.verify.cpp
@@ -408,5 +408,9 @@ void test() {
   // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
   std::ranges::fold_right(iter, iter, 0, std::plus());
   // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::fold_right_last(range, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::fold_right_last(iter, iter, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
 #endif
 }

diff  --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/ranges.fold_right_last.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/ranges.fold_right_last.pass.cpp
new file mode 100644
index 0000000000000..d429762b96dce
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/ranges.fold_right_last.pass.cpp
@@ -0,0 +1,204 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// REQUIRES: std-at-least-c++23
+
+// template<bidirectional_iterator I, sentinel_for<I> S,
+//         indirectly-binary-right-foldable<iter_value_t<I>, I> F>
+//   requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
+//   constexpr auto ranges::fold_right_last(I first, S last, F f);
+
+// template<bidirectional_range R,
+//         indirectly-binary-right-foldable<range_value_t<R>, iterator_t<R>> F>
+//   requires constructible_from<range_value_t<R>, range_reference_t<R>>
+//   constexpr auto ranges::fold_right_last(R&& r, F f);
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <deque>
+#include <functional>
+#include <iterator>
+#include <list>
+#include <optional>
+#include <ranges>
+#include <set>
+#include <string_view>
+#include <string>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_range.h"
+#include "invocable_with_telemetry.h"
+#include "maths.h"
+
+using std::ranges::fold_right_last;
+
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_iterator(R& r, F f, std::optional<Expected> const& expected) {
+  {
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(r.begin(), r.end(), f);
+    assert(result == expected);
+  }
+
+  {
+    auto telemetry                                              = invocable_telemetry();
+    auto f2                                                     = invocable_with_telemetry(f, telemetry);
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(r.begin(), r.end(), f2);
+    assert(result == expected);
+    if (expected.has_value()) {
+      assert(telemetry.invocations == std::ranges::distance(r) - 1);
+      assert(telemetry.moves == 1);
+      assert(telemetry.copies == 1);
+    }
+  }
+}
+
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_lvalue_range(R& r, F f, std::optional<Expected> const& expected) {
+  {
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(r, f);
+    assert(result == expected);
+  }
+
+  {
+    auto telemetry                                              = invocable_telemetry();
+    auto f2                                                     = invocable_with_telemetry(f, telemetry);
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(r, f2);
+    assert(result == expected);
+    if (expected.has_value()) {
+      assert(telemetry.invocations == std::ranges::distance(r) - 1);
+      assert(telemetry.moves == 0);
+      assert(telemetry.copies == 1);
+    }
+  }
+}
+
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_rvalue_range(R& r, F f, std::optional<Expected> const& expected) {
+  {
+    auto r2                                                     = r;
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(std::move(r2), f);
+    assert(result == expected);
+  }
+
+  {
+    auto telemetry                                              = invocable_telemetry();
+    auto f2                                                     = invocable_with_telemetry(f, telemetry);
+    auto r2                                                     = r;
+    std::same_as<std::optional<Expected>> decltype(auto) result = fold_right_last(std::move(r2), f2);
+    assert(result == expected);
+    if (expected.has_value()) {
+      assert(telemetry.invocations == std::ranges::distance(r) - 1);
+      assert(telemetry.moves == 0);
+      assert(telemetry.copies == 1);
+    }
+  }
+}
+
+template <std::ranges::input_range R, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check(R r, F f, std::optional<Expected> const& expected) {
+  check_iterator(r, f, expected);
+  check_lvalue_range(r, f, expected);
+  check_rvalue_range(r, f, expected);
+}
+
+constexpr void empty_range_test_case() {
+  auto const data = std::vector<int>{};
+  check(data, std::plus(), std::optional<int>());
+  check(data, std::multiplies(), std::optional<int>());
+}
+
+constexpr void common_range_test_case() {
+  auto const data = std::vector<int>{1, 2, 3, 4};
+  check(data, std::plus(), std::optional(triangular_sum(data)));
+  check(data, std::multiplies(), std::optional(factorial(data.back())));
+
+  auto multiply_with_next = [n = 1](auto const x, auto const y) mutable {
+    auto const result = x * y * n;
+    n                 = x;
+    return static_cast<std::size_t>(result);
+  };
+  check(data, multiply_with_next, std::optional(factorial(data.size()) * factorial(data.size() - 1)));
+
+  auto fib = [n = 1](auto x, auto) mutable {
+    auto old_x = x;
+    x += n;
+    n = old_x;
+    return x;
+  };
+  check(data, fib, std::optional(fibonacci(data.back())));
+}
+
+constexpr void non_common_range_test_case() {
+  auto parse = [](std::string_view const s) {
+    return s == "zero"  ? 0.0
+         : s == "one"   ? 1.0
+         : s == "two"   ? 2.0
+         : s == "three" ? 3.0
+         : s == "four"  ? 4.0
+         : s == "five"  ? 5.0
+         : s == "six"   ? 6.0
+         : s == "seven" ? 7.0
+         : s == "eight" ? 8.0
+         : s == "nine"  ? 9.0
+                        : (assert(false), 10.0); // the number here is arbitrary
+  };
+
+  {
+    auto data  = std::vector<std::string>{"five", "three", "two", "six", "one", "four"};
+    auto range = data | std::views::transform(parse);
+    check(range, std::plus(), std::optional(triangular_sum(range)));
+  }
+}
+
+constexpr bool test_case() {
+  empty_range_test_case();
+  common_range_test_case();
+  non_common_range_test_case();
+  return true;
+}
+
+// Most containers aren't constexpr
+void runtime_only_test_case() {
+  {
+    auto const data     = std::list<int>{2, 4, 6, 8, 10, 12};
+    auto const expected = triangular_sum(data);
+    check(data, std::plus<long>(), std::optional(static_cast<long>(expected)));
+  }
+
+  {
+    auto const data     = std::deque<double>{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
+    auto plus           = [](double const x, int const y) { return x + y; };
+    auto const expected = -21.1; // -5.5 + int(- 6.6) = -5.5 +  -6 = -11.5
+                                 // -4.4 + int(-11.5) = -4.4 + -11 = -15.4
+                                 // -3.3 + int(-15.4) = -3.3 + -15 = -18.3
+                                 // -2.2 + int(-18.3) = -2.2 + -18 = -20.2
+                                 // -1.1 + int(-20.2) = -1.1 + -20 = -21.1.
+    check(data, plus, std::optional(expected));
+  }
+
+  {
+    auto const data     = std::set<int>{2, 4, 6, 8, 10, 12};
+    auto const expected = triangular_sum(data);
+    check(data, std::plus<long>(), std::optional(static_cast<long>(expected)));
+  }
+}
+
+int main(int, char**) {
+  test_case();
+  static_assert(test_case());
+  runtime_only_test_case();
+  return 0;
+}

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
index beecf87cd4aa7..657875444c687 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
@@ -52,6 +52,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 #  endif
@@ -102,6 +106,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 #  endif
@@ -167,6 +175,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 #  endif
@@ -241,6 +253,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 #  endif
@@ -324,6 +340,13 @@
 #    error "__cpp_lib_ranges_find_last should have the value 202207L in c++23"
 #  endif
 
+#  ifndef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should be defined in c++23"
+#  endif
+#  if __cpp_lib_ranges_fold != 202207L
+#    error "__cpp_lib_ranges_fold should have the value 202207L in c++23"
+#  endif
+
 #  ifndef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should be defined in c++23"
 #  endif
@@ -428,6 +451,13 @@
 #    error "__cpp_lib_ranges_find_last should have the value 202207L in c++26"
 #  endif
 
+#  ifndef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should be defined in c++26"
+#  endif
+#  if __cpp_lib_ranges_fold != 202207L
+#    error "__cpp_lib_ranges_fold should have the value 202207L in c++26"
+#  endif
+
 #  ifndef __cpp_lib_ranges_starts_ends_with
 #    error "__cpp_lib_ranges_starts_ends_with should be defined in c++26"
 #  endif

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index 3f15dd2fd1d06..dfee4b6d458db 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -680,6 +680,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should not be defined before c++26"
 #  endif
@@ -1644,6 +1648,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should not be defined before c++26"
 #  endif
@@ -2773,6 +2781,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should not be defined before c++26"
 #  endif
@@ -4169,6 +4181,10 @@
 #    error "__cpp_lib_ranges_find_last should not be defined before c++23"
 #  endif
 
+#  ifdef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should not be defined before c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should not be defined before c++26"
 #  endif
@@ -5763,6 +5779,13 @@
 #    error "__cpp_lib_ranges_find_last should have the value 202207L in c++23"
 #  endif
 
+#  ifndef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should be defined in c++23"
+#  endif
+#  if __cpp_lib_ranges_fold != 202207L
+#    error "__cpp_lib_ranges_fold should have the value 202207L in c++23"
+#  endif
+
 #  ifdef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should not be defined before c++26"
 #  endif
@@ -7702,6 +7725,13 @@
 #    error "__cpp_lib_ranges_find_last should have the value 202207L in c++26"
 #  endif
 
+#  ifndef __cpp_lib_ranges_fold
+#    error "__cpp_lib_ranges_fold should be defined in c++26"
+#  endif
+#  if __cpp_lib_ranges_fold != 202207L
+#    error "__cpp_lib_ranges_fold should have the value 202207L in c++26"
+#  endif
+
 #  ifndef __cpp_lib_ranges_indices
 #    error "__cpp_lib_ranges_indices should be defined in c++26"
 #  endif

diff  --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index 7c67e7083e88d..080380712cf2a 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -102,6 +102,7 @@ static_assert(test(std::ranges::fold_left_with_iter, a, 0, std::plus()));
 static_assert(test(std::ranges::fold_left_first, a, std::plus()));
 static_assert(test(std::ranges::fold_left_first_with_iter, a, std::plus()));
 static_assert(test(std::ranges::fold_right, a, 0, std::plus()));
+static_assert(test(std::ranges::fold_right_last, a, std::plus()));
 #endif
 static_assert(test(std::ranges::for_each, a, odd));
 static_assert(test(std::ranges::for_each_n, a, 10, odd));

diff  --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 3770f026009ca..2c1f92992bf1a 100644
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -1142,6 +1142,11 @@ def add_version_header(tc):
             "values": {"c++23": 202207},
             "headers": ["algorithm"],
         },
+        {
+            "name": "__cpp_lib_ranges_fold",
+            "values": {"c++23": 202207},
+            "headers": ["algorithm"],
+        },
         {
             "name": "__cpp_lib_ranges_indices",
             "values": {"c++26": 202506},


        


More information about the libcxx-commits mailing list