[libcxx-commits] [libcxx] WIP [libc++][ranges] P3138R3: `views::cache_latest` (PR #151450)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 4 09:21:06 PDT 2026
https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/151450
>From 2676a6363a24738c9433b6018fb73fca92992a86 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Wed, 16 Jul 2025 23:17:31 +0300
Subject: [PATCH 01/15] [libc++][ranges] P3138R3: `views::cache_latest`
Implements: https://wg21.link/P3138R3
Closes #118134
References:
- https://wg21.link/range.cache.latest
- https://wg21.link/range.nonprop.cache
---
libcxx/docs/FeatureTestMacroTable.rst | 2 +
libcxx/docs/ReleaseNotes/24.rst | 1 +
libcxx/docs/Status/Cxx26Papers.csv | 2 +-
libcxx/include/CMakeLists.txt | 1 +
libcxx/include/__ranges/cache_latest_view.h | 228 +++++++++++++++
.../include/__ranges/non_propagating_cache.h | 2 +
libcxx/include/module.modulemap.in | 1 +
libcxx/include/ranges | 8 +
libcxx/include/version | 2 +
libcxx/modules/std/ranges.inc | 9 +
.../range.cache.latest/nodiscard.verify.cpp | 69 +++++
.../ranges/range.nonprop.cache/reset.pass.cpp | 52 ++++
.../ranges.version.compile.pass.cpp | 27 ++
.../version.version.compile.pass.cpp | 27 ++
.../cpo.compile.pass.cpp | 2 +-
.../range.cache.latest/adaptor.pass.cpp | 259 ++++++++++++++++++
.../range.cache.latest/base.pass.cpp | 85 ++++++
.../range.cache.latest/begin.pass.cpp | 107 ++++++++
.../range.cache.latest/ctad.compile.pass.cpp | 30 ++
.../range.cache.latest/ctor.default.pass.cpp | 81 ++++++
.../range.cache.latest/ctor.view.pass.cpp | 24 ++
.../enable_borrowed_range.compile.pass.cpp | 34 +++
.../range.cache.latest/end.pass.cpp | 109 ++++++++
.../range.cache.latest/general.pass.cpp | 75 +++++
.../iterator/ctor.default.pass.cpp | 81 ++++++
.../sentinel/ctor.default.pass.cpp | 81 ++++++
.../range.cache.latest/size.pass.cpp | 109 ++++++++
.../generate_feature_test_macro_components.py | 5 +
28 files changed, 1511 insertions(+), 2 deletions(-)
create mode 100644 libcxx/include/__ranges/cache_latest_view.h
create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
create mode 100644 libcxx/test/libcxx/ranges/range.nonprop.cache/reset.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/adaptor.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/base.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/begin.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctad.compile.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.default.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.view.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/enable_borrowed_range.compile.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/end.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/general.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/iterator/ctor.default.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/sentinel/ctor.default.pass.cpp
create mode 100644 libcxx/test/std/ranges/range.adaptors/range.cache.latest/size.pass.cpp
diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 60af581e122ec..39bbe2cc94722 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -510,6 +510,8 @@ Status
---------------------------------------------------------- -----------------
``__cpp_lib_philox_engine`` *unimplemented*
---------------------------------------------------------- -----------------
+ ``__cpp_lib_ranges_cache_latest`` ``202411L``
+ ---------------------------------------------------------- -----------------
``__cpp_lib_ranges_concat`` ``202403L``
---------------------------------------------------------- -----------------
``__cpp_lib_ranges_indices`` ``202506L``
diff --git a/libcxx/docs/ReleaseNotes/24.rst b/libcxx/docs/ReleaseNotes/24.rst
index f42b5621e9fb5..c5c6386ae0bea 100644
--- a/libcxx/docs/ReleaseNotes/24.rst
+++ b/libcxx/docs/ReleaseNotes/24.rst
@@ -40,6 +40,7 @@ Implemented Papers
- P0493R5: Atomic minimum/maximum (`Github <https://llvm.org/PR105418>`__)
- P3059R2: Making user-defined constructors of view iterators/sentinels private (`Github <https://github.com/llvm/llvm-project/issues/189599>`__)
+- P3138R5: ``views::cache_latest`` (`Github <https://llvm.org/PR118134>`__)
- P0792R14: ``function_ref`` : a type-erased callable reference (`Github <https://llvm.org/PR105376>`__)
- P3948R1: ``constant_wrapper`` is the only tool needed for passing constant expressions via function arguments (`Github <https://llvm.org/PR189604>`__)
- P3961R1: Less double indirection in ``function_ref`` (RU-220) (`Github <https://llvm.org/PR189606>`__)
diff --git a/libcxx/docs/Status/Cxx26Papers.csv b/libcxx/docs/Status/Cxx26Papers.csv
index 1832ec915a06a..500a2fae0cd65 100644
--- a/libcxx/docs/Status/Cxx26Papers.csv
+++ b/libcxx/docs/Status/Cxx26Papers.csv
@@ -77,7 +77,7 @@
"`P2300R10 <https://wg21.link/P2300R10>`__","``std::execution``","2024-06 (St. Louis)","","","`#105440 <https://github.com/llvm/llvm-project/issues/105440>`__",""
"","","","","","",""
"`P3136R1 <https://wg21.link/P3136R1>`__","Retiring niebloids","2024-11 (Wrocław)","|Complete|","14","`#118133 <https://github.com/llvm/llvm-project/issues/118133>`__",""
-"`P3138R5 <https://wg21.link/P3138R5>`__","``views::cache_latest``","2024-11 (Wrocław)","","","`#118134 <https://github.com/llvm/llvm-project/issues/118134>`__",""
+"`P3138R5 <https://wg21.link/P3138R5>`__","``views::cache_latest``","2024-11 (Wrocław)","|Complete|","24","`#118134 <https://github.com/llvm/llvm-project/issues/118134>`__",""
"`P3379R0 <https://wg21.link/P3379R0>`__","Constrain ``std::expected`` equality operators","2024-11 (Wrocław)","|Complete|","21","`#118135 <https://github.com/llvm/llvm-project/issues/118135>`__",""
"`P2862R1 <https://wg21.link/P2862R1>`__","``text_encoding::name()`` should never return null values","2024-11 (Wrocław)","|Complete|","23","`#118371 <https://github.com/llvm/llvm-project/issues/118371>`__",""
"`P2897R7 <https://wg21.link/P2897R7>`__","``aligned_accessor``: An ``mdspan`` accessor expressing pointer over-alignment","2024-11 (Wrocław)","|Complete|","21","`#118372 <https://github.com/llvm/llvm-project/issues/118372>`__",""
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index dc1d8bb26bac4..d5739439fcdaf 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -739,6 +739,7 @@ set(files
__ranges/adjacent_view.h
__ranges/all.h
__ranges/as_rvalue_view.h
+ __ranges/cache_latest_view.h
__ranges/chunk_by_view.h
__ranges/common_view.h
__ranges/concat_view.h
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
new file mode 100644
index 0000000000000..fcf6b22e364a2
--- /dev/null
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -0,0 +1,228 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H
+#define _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H
+
+#include <__concepts/constructible.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iter_swap.h>
+#include <__iterator/iterator_traits.h>
+#include <__memory/addressof.h>
+#include <__ranges/access.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/non_propagating_cache.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/size.h>
+#include <__ranges/view_interface.h>
+#include <__type_traits/add_pointer.h>
+#include <__type_traits/conditional.h>
+#include <__type_traits/is_reference.h>
+#include <__utility/as_lvalue.h>
+#include <__utility/forward.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+namespace ranges {
+
+// [range.cache.latest.view]
+
+template <input_range _View>
+ requires view<_View>
+class cache_latest_view : public view_interface<cache_latest_view<_View>> {
+ _View __base_ = _View();
+ using __cache_t _LIBCPP_NODEBUG =
+ conditional_t<is_reference_v<range_reference_t<_View>>,
+ add_pointer_t<range_reference_t<_View>>,
+ range_reference_t<_View>>;
+
+ __non_propagating_cache<__cache_t> __cache_;
+
+ // [range.cache.latest.iterator], class cache_latest_view::iterator
+ class __iterator;
+ // [range.cache.latest.sentinel], class cache_latest_view::sentinel
+ class __sentinel;
+
+public:
+ _LIBCPP_HIDE_FROM_ABI cache_latest_view()
+ requires default_initializable<_View>
+ = default;
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit cache_latest_view(_View __base) : __base_{std::move(__base)} {}
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+ requires copy_constructible<_View>
+ {
+ return __base_;
+ }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator(*this); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return __sentinel{*this}; }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ requires sized_range<_View>
+ {
+ return ranges::size(__base_);
+ }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ requires sized_range<const _View>
+ {
+ return ranges::size(__base_);
+ }
+
+ // TODO: Implement when P2846R6 is available.
+ // constexpr auto reserve_hint()
+ // requires approximately_sized_range<_View>
+ // {
+ // return ranges::reserve_hint(__base_);
+ // }
+ // constexpr auto reserve_hint() const
+ // requires approximately_sized_range<const _View>
+ // {
+ // return ranges::reserve_hint(__base_);
+ // }
+};
+
+template <class _Range>
+cache_latest_view(_Range&&) -> cache_latest_view<views::all_t<_Range>>;
+
+// [range.cache.latest.iterator]
+
+template <input_range _View>
+ requires view<_View>
+class cache_latest_view<_View>::__iterator {
+ cache_latest_view* __parent_;
+ iterator_t<_View> __current_;
+
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(cache_latest_view& __parent)
+ : __parent_{std::addressof(__parent)}, __current_{ranges::begin(__parent.__base_)} {}
+
+ friend class cache_latest_view<_View>;
+
+public:
+ using difference_type = range_difference_t<_View>;
+ using value_type = range_value_t<_View>;
+ using iterator_concept = input_iterator_tag;
+
+ _LIBCPP_HIDE_FROM_ABI __iterator(__iterator&&) = default;
+ _LIBCPP_HIDE_FROM_ABI __iterator& operator=(__iterator&&) = default;
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() && { return std::move(__current_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_View>& base() const& noexcept { return __current_; }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_reference_t<_View>& operator*() const {
+ if constexpr (is_reference_v<range_reference_t<_View>>) {
+ if (!__parent_->__cache_.__has_value()) {
+ __parent_->__cache_.__emplace(std::addressof(std::__as_lvalue(*__current_)));
+ }
+ return **__parent_->__cache_;
+ } else {
+ if (!__parent_->__cache_.__has_value()) {
+ __parent_->__cache_.__emplace_from([&]() -> decltype(auto) { return *__current_; });
+ }
+ return *__parent_->__cache_;
+ }
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
+ __parent_->__cache_.__reset();
+ ++__current_;
+ return *this;
+ }
+ _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_rvalue_reference_t<_View>
+ iter_move(const __iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__current_))) {
+ return ranges::iter_move(__i.__current_);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend constexpr void
+ iter_swap(const __iterator& __x,
+ const __iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
+ requires indirectly_swappable<iterator_t<_View>>
+ {
+ ranges::iter_swap(__x.__current_, __y.__current_);
+ }
+};
+
+// [range.cache.latest.sentinel]
+
+template <input_range _View>
+ requires view<_View>
+class cache_latest_view<_View>::__sentinel {
+ sentinel_t<_View> __end_ = sentinel_t<_View>();
+
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(cache_latest_view& __parent)
+ : __end_{ranges::end(__parent.__base_)} {}
+
+ friend class cache_latest_view<_View>;
+
+public:
+ _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_View> base() const { return __end_; }
+
+ _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {
+ return __x.__current_ == __y.__end_;
+ }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<_View>
+ operator-(const __iterator& __x, const __sentinel& __y)
+ requires sized_sentinel_for<sentinel_t<_View>, iterator_t<_View>>
+ {
+ return __x.__current_ - __y.__end_;
+ }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<_View>
+ operator-(const __sentinel& __x, const __iterator& __y)
+ requires sized_sentinel_for<sentinel_t<_View>, iterator_t<_View>>
+ {
+ return __x.__end_ - __y.__current_;
+ }
+};
+
+namespace views {
+namespace __cache_latest_view {
+
+struct __fn : __range_adaptor_closure<__fn> {
+ template <class _Range>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto
+ operator()(_Range&& __range) noexcept(noexcept(/**/ cache_latest_view(std::forward<_Range>(__range))))
+ -> decltype(/*-------------------------------*/ cache_latest_view(std::forward<_Range>(__range))) {
+ return /*--------------------------------------*/ cache_latest_view(std::forward<_Range>(__range));
+ }
+};
+
+} // namespace __cache_latest_view
+
+inline namespace __cpo {
+inline constexpr auto cache_latest = __cache_latest_view::__fn{};
+} // namespace __cpo
+} // namespace views
+} // namespace ranges
+
+#endif // _LIBCPP_STD_VER >= 26
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_TO_CACHE_LATEST_VIEW_H
diff --git a/libcxx/include/__ranges/non_propagating_cache.h b/libcxx/include/__ranges/non_propagating_cache.h
index c2bfc443faa18..d0dd983cc33a4 100644
--- a/libcxx/include/__ranges/non_propagating_cache.h
+++ b/libcxx/include/__ranges/non_propagating_cache.h
@@ -92,6 +92,8 @@ class __non_propagating_cache {
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace(_Args&&... __args) {
return __value_.emplace(__forward_tag{}, std::forward<_Args>(__args)...).__t_;
}
+
+ _LIBCPP_HIDE_FROM_ABI constexpr void __reset() { __value_.reset(); }
};
struct __empty_cache {};
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index f56d48784737b..ff07312f83397 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1926,6 +1926,7 @@ module std {
export std.ranges.ref_view
}
module as_rvalue_view { header "__ranges/as_rvalue_view.h" }
+ module cache_latest_view { header "__ranges/cache_latest_view.h" }
module chunk_by_view {
header "__ranges/chunk_by_view.h"
export std.functional.bind_back
diff --git a/libcxx/include/ranges b/libcxx/include/ranges
index b764769095a86..78c2e8904dd09 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -428,6 +428,13 @@ namespace std::ranges {
enable_borrowed_range<V>; // C++23
namespace views { inline constexpr unspecified stride = unspecified; } // C++23
+
+ // [range.cache.latest], cache latest view
+ template<input_range V>
+ requires view<V>
+ class cache_latest_view; // C++26
+
+ namespace views { inline constexpr unspecified cache_latest = unspecified; } // C++26
}
namespace std {
@@ -525,6 +532,7 @@ namespace std {
# endif
# if _LIBCPP_STD_VER >= 26
+# include <__ranges/cache_latest_view.h>
# include <__ranges/concat_view.h>
# endif
diff --git a/libcxx/include/version b/libcxx/include/version
index 5d0b96a1fe230..fd265e4bb55e6 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -208,6 +208,7 @@ __cpp_lib_ranges 202406L <algorithm> <fun
202110L // C++20
__cpp_lib_ranges_as_const 202207L <ranges>
__cpp_lib_ranges_as_rvalue 202207L <ranges>
+__cpp_lib_ranges_cache_latest 202411L <ranges>
__cpp_lib_ranges_chunk 202202L <ranges>
__cpp_lib_ranges_chunk_by 202202L <ranges>
__cpp_lib_ranges_concat 202403L <ranges>
@@ -619,6 +620,7 @@ __cpp_lib_void_t 201411L <type_traits>
# undef __cpp_lib_out_ptr
# define __cpp_lib_out_ptr 202311L
// # define __cpp_lib_philox_engine 202406L
+# define __cpp_lib_ranges_cache_latest 202411L
# define __cpp_lib_ranges_concat 202403L
# define __cpp_lib_ranges_indices 202506L
# define __cpp_lib_ratio 202306L
diff --git a/libcxx/modules/std/ranges.inc b/libcxx/modules/std/ranges.inc
index 876942bdffbf5..0434fbc86a829 100644
--- a/libcxx/modules/std/ranges.inc
+++ b/libcxx/modules/std/ranges.inc
@@ -372,6 +372,15 @@ export namespace std {
using std::ranges::views::enumerate;
}
#endif // _LIBCPP_STD_VER >= 23
+
+#if _LIBCPP_STD_VER >= 26
+ // [range.cache.latest], Cache latest view
+ using std::ranges::cache_latest_view;
+
+ namespace views {
+ using std::ranges::views::cache_latest;
+ }
+#endif
} // namespace ranges
namespace views = ranges::views;
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..1fa32a0375d07
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <utility>
+#include <vector>
+
+void test() {
+ std::vector<int> range;
+ std::ranges::cache_latest_view clv{range};
+
+ // [range.cache.latest.view]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(clv).base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(clv).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ clv.begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ clv.end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ clv.size();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(clv).size();
+
+ // [range.cache.latest.iterator]
+
+ auto it = clv.begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(it).base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(it).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *(std::as_const(it));
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ iter_move(it);
+
+ // [range.cache.latest.sentinel]
+
+ auto st = clv.end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(st).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - st;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st - it;
+
+ // [range.enumerate.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::cache_latest(range);
+}
\ No newline at end of file
diff --git a/libcxx/test/libcxx/ranges/range.nonprop.cache/reset.pass.cpp b/libcxx/test/libcxx/ranges/range.nonprop.cache/reset.pass.cpp
new file mode 100644
index 0000000000000..be00d58359245
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.nonprop.cache/reset.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// constexpr void __reset();
+
+#include <ranges>
+
+#include <cassert>
+
+template <class T>
+constexpr void test() {
+ using Cache = std::ranges::__non_propagating_cache<T>;
+
+ // __reset on an empty cache
+ {
+ Cache cache;
+ assert(!cache.__has_value());
+ cache.__reset();
+ assert(!cache.__has_value());
+ }
+
+ // __reset on a non-empty cache
+ {
+ Cache cache;
+ cache.__emplace();
+ assert(cache.__has_value());
+ cache.__reset();
+ assert(!cache.__has_value());
+ }
+}
+
+struct T {};
+
+constexpr bool tests() {
+ test<T>();
+ test<int>();
+ return true;
+}
+
+int main(int, char**) {
+ tests();
+ static_assert(tests());
+
+ return 0;
+}
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
index 670b0e664721a..1364468dc596e 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
@@ -36,6 +36,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -98,6 +102,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -160,6 +168,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -225,6 +237,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -302,6 +318,10 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++23"
@@ -427,6 +447,13 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++26"
# endif
+# ifndef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should be defined in c++26"
+# endif
+# if __cpp_lib_ranges_cache_latest != 202411L
+# error "__cpp_lib_ranges_cache_latest should have the value 202411L in c++26"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++26"
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 9143d715ff7d7..28b0630c6412e 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
@@ -664,6 +664,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -1640,6 +1644,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -2781,6 +2789,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -4189,6 +4201,10 @@
# error "__cpp_lib_ranges_as_rvalue should not be defined before c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# ifdef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should not be defined before c++23"
# endif
@@ -5774,6 +5790,10 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++23"
# endif
+# ifdef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should not be defined before c++26"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++23"
@@ -7713,6 +7733,13 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++26"
# endif
+# ifndef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should be defined in c++26"
+# endif
+# if __cpp_lib_ranges_cache_latest != 202411L
+# error "__cpp_lib_ranges_cache_latest should have the value 202411L in c++26"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++26"
diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp
index 71a0b6ebb804c..c0cb53029a349 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp
@@ -136,7 +136,7 @@ static_assert(test(std::views::zip, a, a));
#endif
#if TEST_STD_VER >= 26
-// static_assert(test(std::views::cache_latest, a));
+static_assert(test(std::views::cache_latest, a));
static_assert(test(std::views::concat, a, a));
// static_assert(test(std::views::to_input, a));
#endif
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/adaptor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/adaptor.pass.cpp
new file mode 100644
index 0000000000000..779c59436c21d
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/adaptor.pass.cpp
@@ -0,0 +1,259 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "test_range.h"
+
+static_assert(std::is_same_v<decltype(std::views::cache_latest), decltype(std::ranges::views::cache_latest)>);
+
+struct NonView {};
+
+static_assert(std::default_initializable<NonView>);
+
+struct DefaultInitializableView : std::ranges::view_base {
+ int i_;
+
+ int* begin();
+ int* end();
+};
+
+static_assert(std::default_initializable<DefaultInitializableView>);
+static_assert(std::ranges::common_range<DefaultInitializableView>);
+static_assert(std::ranges::input_range<DefaultInitializableView>);
+
+struct CommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr forward_iterator<int*> end() { return begin(); }
+};
+
+static_assert(std::ranges::common_range<CommonView>);
+static_assert(std::ranges::forward_range<CommonView>);
+static_assert(std::ranges::input_range<CommonView>);
+
+struct NonCommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr sentinel_wrapper<forward_iterator<int*>> end() { return sentinel_wrapper<forward_iterator<int*>>(begin()); }
+};
+
+static_assert(!std::ranges::common_range<NonCommonView>);
+static_assert(std::ranges::forward_range<NonCommonView>);
+static_assert(std::ranges::input_range<NonCommonView>);
+static_assert(
+ std::derived_from< typename std::iterator_traits<std::ranges::iterator_t<NonCommonView>>::iterator_category,
+ std::input_iterator_tag>);
+
+// Check that the `cache_latest` adaptor can be used with a view.
+
+static_assert(!std::is_invocable_v<decltype(std::views::cache_latest)>);
+static_assert(!std::is_invocable_v<decltype(std::views::cache_latest), NonView>);
+static_assert(std::is_invocable_v<decltype(std::views::cache_latest), DefaultInitializableView>);
+static_assert(std::is_invocable_v<decltype(std::views::cache_latest), CommonView>);
+static_assert(std::is_invocable_v<decltype(std::views::cache_latest), NonCommonView>);
+
+static_assert(!CanBePiped<NonView, decltype(std::views::cache_latest)>);
+static_assert(CanBePiped<DefaultInitializableView&, decltype(std::views::cache_latest)>);
+static_assert(CanBePiped<CommonView&, decltype(std::views::cache_latest)>);
+static_assert(CanBePiped<NonCommonView&, decltype(std::views::cache_latest)>);
+
+constexpr bool test() {
+ { // view | views::cache_latest
+ {
+ DefaultInitializableView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<DefaultInitializableView>> decltype(auto) v =
+ view | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ {
+ CommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<CommonView>> decltype(auto) v = view | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ {
+ NonCommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<NonCommonView>> decltype(auto) v = view | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ }
+
+ { // adaptor | views::cache_latest
+ {
+ DefaultInitializableView view{{}, 94};
+ const auto partial = std::views::transform(std::identity{}) | std::views::cache_latest;
+ std::same_as<std::ranges::cache_latest_view<
+ std::ranges::transform_view<DefaultInitializableView, std::identity>>> decltype(auto) v = partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ {
+ CommonView view{{}, 94};
+ const auto partial = std::views::transform(std::identity{}) | std::views::cache_latest;
+ std::same_as<
+ std::ranges::cache_latest_view< std::ranges::transform_view<CommonView, std::identity>>> decltype(auto) v =
+ partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ {
+ NonCommonView view{{}, 94};
+ const auto partial = std::views::transform(std::identity{}) | std::views::cache_latest;
+ std::same_as<
+ std::ranges::cache_latest_view< std::ranges::transform_view<NonCommonView, std::identity>>> decltype(auto) v =
+ partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(std::ranges::input_range<decltype(v)>);
+ }
+ }
+
+ { // views::cache_latest | adaptor
+ {
+ DefaultInitializableView view{{}, 94};
+ const auto partial = std::views::cache_latest | std::views::transform(std::identity{});
+ std::same_as<std::ranges::transform_view<std::ranges::cache_latest_view<DefaultInitializableView>,
+ std::identity>> decltype(auto) v = partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ CommonView view{{}, 94};
+ const auto partial = std::views::cache_latest | std::views::transform(std::identity{});
+ std::same_as<
+ std::ranges::transform_view<std::ranges::cache_latest_view<CommonView>, std::identity>> decltype(auto) v =
+ partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ NonCommonView view{{}, 94};
+ const auto partial = std::views::cache_latest | std::views::transform(std::identity{});
+ std::same_as<
+ std::ranges::transform_view<std::ranges::cache_latest_view<NonCommonView>, std::identity>> decltype(auto) v =
+ partial(view);
+ assert(v.base().base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ }
+
+ { // views::cache_latest | views::all
+ {
+ DefaultInitializableView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<DefaultInitializableView>> decltype(auto) v =
+ std::views::all(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ CommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<CommonView>> decltype(auto) v =
+ std::views::all(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ NonCommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<NonCommonView>> decltype(auto) v =
+ std::views::all(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ }
+
+ { // views::cache_latest | views::all_t
+ {
+ DefaultInitializableView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<DefaultInitializableView>> decltype(auto) v =
+ std::views::all_t<DefaultInitializableView>(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ CommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<CommonView>> decltype(auto) v =
+ std::views::all_t<CommonView>(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ {
+ NonCommonView view{{}, 94};
+ std::same_as<std::ranges::cache_latest_view<NonCommonView>> decltype(auto) v =
+ std::views::all_t<NonCommonView>(view) | std::views::cache_latest;
+ assert(v.base().i_ == 94);
+
+ static_assert(std::ranges::input_range<decltype(v)>);
+ static_assert(!std::ranges::forward_range<decltype(v)>);
+ static_assert(!std::ranges::common_range<decltype(v)>);
+ }
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/base.pass.cpp
new file mode 100644
index 0000000000000..d0734633bb9c3
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/base.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// constexpr V base() const & requires copy_constructible<V> { return base_; }
+// constexpr V base() && { return std::move(base_); }
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+
+#include <utility>
+
+#include "MoveOnly.h"
+
+struct SimpleView : std::ranges::view_base {
+ int i_;
+
+ int* begin() const;
+ int* end() const;
+};
+
+struct MoveOnlyView : public SimpleView {
+ MoveOnly m_;
+};
+
+template <class T>
+concept HasBase = requires(T&& t) { std::forward<T>(t).base(); };
+
+static_assert(HasBase<std::ranges::cache_latest_view<SimpleView> const&>);
+static_assert(HasBase<std::ranges::cache_latest_view<SimpleView>&&>);
+
+static_assert(!HasBase<std::ranges::cache_latest_view<MoveOnlyView> const&>);
+static_assert(HasBase<std::ranges::cache_latest_view<MoveOnlyView>&&>);
+
+constexpr bool test() {
+ { // &
+ std::ranges::cache_latest_view<SimpleView> view(SimpleView{{}, 94});
+ std::same_as<SimpleView> decltype(auto) v = view.base();
+ assert(v.i_ == 94);
+ }
+
+ { // const &
+ const std::ranges::cache_latest_view<SimpleView> view(SimpleView{{}, 94});
+ std::same_as<SimpleView> decltype(auto) v = view.base();
+ assert(v.i_ == 94);
+ }
+
+ { // &&
+ std::ranges::cache_latest_view<SimpleView> view(SimpleView{{}, 94});
+ std::same_as<SimpleView> decltype(auto) v = std::move(view).base();
+ assert(v.i_ == 94);
+ }
+
+ { // const &&
+ const std::ranges::cache_latest_view<SimpleView> view(SimpleView{{}, 94});
+ std::same_as<SimpleView> decltype(auto) v = std::move(view).base();
+ assert(v.i_ == 94);
+ }
+
+ { // move only
+ std::ranges::cache_latest_view<MoveOnlyView> view(MoveOnlyView{{}, 94});
+ std::same_as<MoveOnlyView> decltype(auto) v = std::move(view).base();
+ assert(v.m_.get() == 94);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/begin.pass.cpp
new file mode 100644
index 0000000000000..3459ce015ef65
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/begin.pass.cpp
@@ -0,0 +1,107 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// constexpr auto begin();
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+
+#include "test_iterators.h"
+
+struct NonConstView : std::ranges::view_base {
+ int* begin();
+ int* end();
+};
+
+struct ConstView : std::ranges::view_base {
+ int* begin() const;
+ int* end() const;
+};
+
+struct ConstNonConstView : std::ranges::view_base {
+ int* begin();
+ int* end();
+ int* begin() const;
+ int* end() const;
+};
+
+template <class T>
+concept HasBegin = requires(T t) { t.begin(); };
+
+static_assert(HasBegin<std::ranges::cache_latest_view<NonConstView>>);
+static_assert(!HasBegin<const std::ranges::cache_latest_view<NonConstView>>);
+static_assert(HasBegin<std::ranges::cache_latest_view<ConstView>>);
+static_assert(!HasBegin<const std::ranges::cache_latest_view<ConstView>>);
+static_assert(HasBegin<std::ranges::cache_latest_view<ConstNonConstView>>);
+static_assert(!HasBegin<const std::ranges::cache_latest_view<ConstNonConstView>>);
+
+struct CommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr forward_iterator<int*> end() { return begin(); }
+};
+
+static_assert(std::ranges::common_range<CommonView>);
+static_assert(std::ranges::forward_range<CommonView>);
+static_assert(std::ranges::input_range<CommonView>);
+
+struct NonCommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr sentinel_wrapper<forward_iterator<int*>> end() { return sentinel_wrapper<forward_iterator<int*>>(begin()); }
+};
+
+static_assert(!std::ranges::common_range<NonCommonView>);
+static_assert(std::ranges::forward_range<NonCommonView>);
+static_assert(std::ranges::input_range<NonCommonView>);
+static_assert(
+ std::derived_from<typename std::iterator_traits<std::ranges::iterator_t<NonCommonView>>::iterator_category,
+ std::input_iterator_tag>);
+
+constexpr bool test() {
+ {
+ CommonView view{{}, 94};
+
+ std::same_as<std::ranges::cache_latest_view<CommonView>> decltype(auto) v = view | std::views::cache_latest;
+ assert(*v.begin() == view.i_);
+ // assert(&*v.begin() == std::addressof(view.i_));
+ }
+ {
+ NonCommonView view{{}, 94};
+
+ std::same_as<std::ranges::cache_latest_view<NonCommonView>> decltype(auto) v = view | std::views::cache_latest;
+ assert(*v.begin() == view.i_);
+ // assert(base(v.begin().base()) == std::addressof(view.i_));
+ }
+
+ {
+ int arr[] = {82, 94, 76};
+
+ std::same_as<std::ranges::cache_latest_view<std::ranges::ref_view<int[3]>>> decltype(auto) v =
+ arr | std::views::cache_latest;
+ assert(base(v.begin().base()) == arr);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctad.compile.pass.cpp
new file mode 100644
index 0000000000000..b42398e4ddb4d
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctad.compile.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// template<class R>
+// cache_latest_view(R&&) -> cache_latest_view<views::all_t<R>>;
+
+#include <concepts>
+#include <ranges>
+#include <utility>
+#include <vector>
+
+static_assert(std::same_as<decltype(std::ranges::cache_latest_view(std::vector<int>{})),
+ std::ranges::cache_latest_view<std::views::all_t<std::vector<int>>>>);
+
+static_assert(std::same_as<decltype(std::ranges::cache_latest_view(std::declval<std::vector<int>&>())),
+ std::ranges::cache_latest_view<std::views::all_t<std::vector<int>&>>>);
+
+static_assert(std::same_as<decltype(std::ranges::cache_latest_view(std::ranges::empty_view<int>{})),
+ std::ranges::cache_latest_view<std::ranges::empty_view<int>>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.default.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.default.pass.cpp
new file mode 100644
index 0000000000000..adae9479188d3
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.default.pass.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// cache_latest_view() requires default_initializable<V> = default;
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+
+struct DefaultInitializableView : std::ranges::view_base {
+ int i_ = 94;
+
+ int* begin();
+ int* end();
+};
+
+static_assert(std::default_initializable<DefaultInitializableView>);
+static_assert(std::default_initializable<std::ranges::cache_latest_view<DefaultInitializableView>>);
+
+struct NoDefaultInitializableView : std::ranges::view_base {
+ NoDefaultInitializableView() = delete;
+
+ int* begin();
+ int* end();
+};
+
+static_assert(!std::default_initializable<NoDefaultInitializableView>);
+static_assert(!std::default_initializable<std::ranges::cache_latest_view<NoDefaultInitializableView>>);
+
+struct NoexceptView : std::ranges::view_base {
+ NoexceptView() noexcept;
+
+ int const* begin() const;
+ int const* end() const;
+};
+
+static_assert(noexcept(std::ranges::cache_latest_view<NoexceptView>()));
+
+struct NoNoexceptView : std::ranges::view_base {
+ NoNoexceptView() noexcept(false);
+
+ int const* begin() const;
+ int const* end() const;
+};
+
+static_assert(!noexcept(std::ranges::cache_latest_view<NoNoexceptView>()));
+
+constexpr bool test() {
+ { // value-initialized (i.e., whether T() is well-formed).
+ std::ranges::cache_latest_view<DefaultInitializableView> cache_latest_view{};
+ assert(cache_latest_view.base().i_ == 94);
+ }
+ { // direct-list-initialized from an empty initializer list (i.e., whether T{} is well-formed).
+ std::ranges::cache_latest_view<DefaultInitializableView> cache_latest_view = {};
+ assert(cache_latest_view.base().i_ == 94);
+ }
+ { // default-initialized (i.e., whether T t; is well-formed).
+ std::ranges::cache_latest_view<DefaultInitializableView> cache_latest_view;
+ assert(cache_latest_view.base().i_ == 94);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.view.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.view.pass.cpp
new file mode 100644
index 0000000000000..9803e7500e8d3
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/ctor.view.pass.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// constexpr explicit cache_latest_view(V base);
+
+constexpr bool test() { return true; }
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/enable_borrowed_range.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/enable_borrowed_range.compile.pass.cpp
new file mode 100644
index 0000000000000..ba721291c3be4
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/enable_borrowed_range.compile.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// Test that std::ranges::cache_latest_view is not std::ranges::borrowed_range.
+
+#include <ranges>
+
+struct BorrowedView : std::ranges::view_base {
+ int* begin();
+ int* end();
+};
+
+template <>
+inline constexpr bool std::ranges::enable_borrowed_range<BorrowedView> = true;
+
+static_assert(!std::ranges::borrowed_range<std::ranges::cache_latest_view<BorrowedView>>);
+
+struct NonBorrowedView : std::ranges::view_base {
+ int* begin();
+ int* end();
+};
+
+static_assert(!std::ranges::borrowed_range<std::ranges::cache_latest_view<NonBorrowedView>>);
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/end.pass.cpp
new file mode 100644
index 0000000000000..f3bf777c35a42
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/end.pass.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// constexpr auto end();
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+
+#include "test_iterators.h"
+#include <print>
+
+struct NonConstView : std::ranges::view_base {
+ int* begin();
+ int* end();
+};
+
+struct ConstView : std::ranges::view_base {
+ int* begin() const;
+ int* end() const;
+};
+
+struct ConstNonConstView : std::ranges::view_base {
+ int* begin();
+ int* end();
+ int* begin() const;
+ int* end() const;
+};
+
+template <class T>
+concept HasEnd = requires(T t) { t.end(); };
+
+static_assert(HasEnd<std::ranges::cache_latest_view<NonConstView>>);
+static_assert(!HasEnd<const std::ranges::cache_latest_view<NonConstView>>);
+static_assert(HasEnd<std::ranges::cache_latest_view<ConstView>>);
+static_assert(!HasEnd<const std::ranges::cache_latest_view<ConstView>>);
+static_assert(HasEnd<std::ranges::cache_latest_view<ConstNonConstView>>);
+static_assert(!HasEnd<const std::ranges::cache_latest_view<ConstNonConstView>>);
+
+struct CommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr forward_iterator<int*> end() { return begin(); }
+};
+
+static_assert(std::ranges::common_range<CommonView>);
+static_assert(std::ranges::forward_range<CommonView>);
+static_assert(std::ranges::input_range<CommonView>);
+
+struct NonCommonView : std::ranges::view_base {
+ int i_;
+
+ constexpr forward_iterator<int*> begin() { return forward_iterator<int*>(&i_); }
+ constexpr sentinel_wrapper<forward_iterator<int*>> end() { return sentinel_wrapper<forward_iterator<int*>>(begin()); }
+};
+
+static_assert(!std::ranges::common_range<NonCommonView>);
+static_assert(std::ranges::forward_range<NonCommonView>);
+static_assert(std::ranges::input_range<NonCommonView>);
+static_assert(
+ std::derived_from<typename std::iterator_traits<std::ranges::iterator_t<NonCommonView>>::iterator_category,
+ std::input_iterator_tag>);
+
+constexpr bool test() {
+ {
+ CommonView view{{}, 94};
+
+ std::same_as<std::ranges::cache_latest_view<CommonView>> decltype(auto) v = view | std::views::cache_latest;
+
+ assert(*base(v.end().base()) == view.i_);
+ }
+ {
+ NonCommonView view{{}, 94};
+
+ std::same_as<std::ranges::cache_latest_view<NonCommonView>> decltype(auto) v = view | std::views::cache_latest;
+
+ assert(*base(v.end().base()) == view.i_);
+ }
+
+ {
+ int arr[] = {82, 94, 76};
+
+ std::same_as<std::ranges::cache_latest_view<std::ranges::ref_view<int[3]>>> decltype(auto) v =
+ arr | std::views::cache_latest;
+
+ assert(base(v.end().base()) == arr + 3);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/general.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/general.pass.cpp
new file mode 100644
index 0000000000000..82f91770a59e8
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/general.pass.cpp
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// Functional tests of std::ranges::cache_latest_view.
+
+#include <cassert>
+#include <ranges>
+#include <vector>
+#include <algorithm>
+
+constexpr bool test() {
+ // Motivational example from P3138R3.
+ std::vector<int> vec = {1, 2, 3, 4, 5};
+ std::vector<int> filtered_vec = {4, 16};
+
+ { // Uncached transform and filter.
+ int transform_counter = 0;
+ const auto get_square = [&](int i) {
+ ++transform_counter;
+ return i * i;
+ };
+ auto view = vec | std::views::transform(get_square) | std::views::filter([&](int i) { return i % 2 == 0; });
+
+ assert(std::ranges::equal(view, filtered_vec));
+ // The transform is called twice for each element that is not filtered out.
+ assert(transform_counter == 7);
+ }
+ { // Cached transform and filter.
+ int transform_counter = 0;
+ const auto get_square = [&](int i) {
+ ++transform_counter;
+ return i * i;
+ };
+ auto view = vec | std::views::transform(get_square) | std::views::cache_latest |
+ std::views::filter([&](int i) { return i % 2 == 0; });
+
+ assert(std::ranges::equal(view, filtered_vec));
+ // The transform is called only once for each element.
+ assert(transform_counter == 5);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/iterator/ctor.default.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/iterator/ctor.default.pass.cpp
new file mode 100644
index 0000000000000..8232d0401ab37
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/iterator/ctor.default.pass.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// template<input_range V>
+// requires view<V>
+// template<bool Const>
+// class to_input_view<V>::iterator
+
+// iterator() requires default_initializable<iterator_t<Base>> = default;
+
+#include <cassert>
+#include <cstdint>
+#include <ranges>
+#include <type_traits>
+
+#include "test_iterators.h"
+
+struct DefaultInitializableIterator {
+ int i_; // Deliberately uninitialized.
+
+ using difference_type = std::intptr_t;
+ using value_type = int;
+ using iterator_category = std::random_access_iterator_tag;
+
+ constexpr int operator*() const { return i_; }
+
+ constexpr DefaultInitializableIterator& operator++() { return *this; }
+ constexpr void operator++(int) {}
+
+ friend constexpr bool operator==(const DefaultInitializableIterator&, const DefaultInitializableIterator&) = default;
+};
+
+static_assert(std::default_initializable<DefaultInitializableIterator>);
+
+struct DefaultInitializableIteratorView : std::ranges::view_base {
+ DefaultInitializableIterator begin() const;
+ DefaultInitializableIterator end() const;
+};
+
+struct NonDefaultInitializableIteratorView : std::ranges::view_base {
+ static_assert(!std::default_initializable<cpp20_input_iterator<int*>>);
+
+ cpp20_input_iterator<int*> begin() const;
+ sentinel_wrapper<cpp20_input_iterator<int*>> end() const;
+};
+
+template <class View>
+using CacheLatestViewIteratorT = std::ranges::iterator_t<std::ranges::cache_latest_view<View>>;
+
+// Check that the to_input_view's iterator is default initializable when the underlying iterator is.
+// static_assert(std::default_initializable<CacheLatestViewIteratorT<DefaultInitializableIteratorView>>);
+// static_assert(!std::default_initializable<CacheLatestViewIteratorT<NonDefaultInitializableIteratorView>>);
+
+constexpr bool test() {
+ // {
+ // CacheLatestViewIteratorT<DefaultInitializableIteratorView> it;
+ // assert(*it == 0); // DefaultInitializableIterator has to be initialized to have value 0.
+ // }
+ // {
+ // CacheLatestViewIteratorT<DefaultInitializableIteratorView> it = {};
+ // assert(*it == 0); // DefaultInitializableIterator has to be initialized to have value 0.
+ // }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/sentinel/ctor.default.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/sentinel/ctor.default.pass.cpp
new file mode 100644
index 0000000000000..7d228552d0900
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/sentinel/ctor.default.pass.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// template<input_range V>
+// requires view<V>
+// template<bool Const>
+// class to_input_view<V>::iterator
+
+// iterator() requires default_initializable<iterator_t<Base>> = default;
+
+#include <cassert>
+#include <cstdint>
+#include <ranges>
+#include <type_traits>
+
+#include "test_iterators.h"
+
+struct DefaultInitializableIterator {
+ int i_; // Deliberately uninitialized.
+
+ using difference_type = std::intptr_t;
+ using value_type = int;
+ using iterator_category = std::random_access_iterator_tag;
+
+ constexpr int operator*() const { return i_; }
+
+ constexpr DefaultInitializableIterator& operator++() { return *this; }
+ constexpr void operator++(int) {}
+
+ friend constexpr bool operator==(const DefaultInitializableIterator&, const DefaultInitializableIterator&) = default;
+};
+
+static_assert(std::default_initializable<DefaultInitializableIterator>);
+
+struct DefaultInitializableIteratorView : std::ranges::view_base {
+ DefaultInitializableIterator begin() const;
+ DefaultInitializableIterator end() const;
+};
+
+struct NonDefaultInitializableIteratorView : std::ranges::view_base {
+ static_assert(!std::default_initializable<cpp20_input_iterator<int*>>);
+
+ cpp20_input_iterator<int*> begin() const;
+ sentinel_wrapper<cpp20_input_iterator<int*>> end() const;
+};
+
+template <class View>
+using CacheLatestViewSentinelT = std::ranges::sentinel_t<std::ranges::cache_latest_view<View>>;
+
+// Check that the to_input_view's iterator is default initializable when the underlying iterator is.
+// static_assert(std::default_initializable<CacheLatestViewSentinelT<DefaultInitializableIteratorView>>);
+// static_assert(!std::default_initializable<CacheLatestViewSentinelT<NonDefaultInitializableIteratorView>>);
+
+constexpr bool test() {
+ // {
+ // CacheLatestViewSentinelT<DefaultInitializableIteratorView> it;
+ // assert(*it == 0); // DefaultInitializableIterator has to be initialized to have value 0.
+ // }
+ // {
+ // CacheLatestViewSentinelT<DefaultInitializableIteratorView> it = {};
+ // assert(*it == 0); // DefaultInitializableIterator has to be initialized to have value 0.
+ // }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.cache.latest/size.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/size.pass.cpp
new file mode 100644
index 0000000000000..229e70c0fc361
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.cache.latest/size.pass.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <ranges>
+
+// class cache_latest_view
+
+// constexpr auto size() requires sized_range<V>;
+// constexpr auto size() const requires sized_range<const V>;
+
+#include <array>
+#include <cassert>
+#include <ranges>
+#include <utility>
+
+#include "test_iterators.h"
+
+template <class T>
+concept HasSize = requires(T t) { t.size(); };
+
+constexpr bool test() {
+ {
+ struct SubtractableIteratorsView : std::ranges::view_base {
+ forward_iterator<int*> begin();
+ sized_sentinel<forward_iterator<int*>> end();
+ };
+
+ using ToCacheLatestViewT = std::ranges::cache_latest_view<SubtractableIteratorsView>;
+
+ static_assert(std::ranges::sized_range<ToCacheLatestViewT&>);
+ static_assert(!std::ranges::range<const ToCacheLatestViewT&>); // no begin()/end()
+
+ static_assert(HasSize<ToCacheLatestViewT&>);
+ static_assert(HasSize<ToCacheLatestViewT&&>);
+ static_assert(!HasSize<const ToCacheLatestViewT&>);
+ static_assert(!HasSize<const ToCacheLatestViewT&&>);
+ }
+ {
+ struct NonSizedView : std::ranges::view_base {
+ bidirectional_iterator<int*> begin();
+ bidirectional_iterator<int*> end();
+ };
+
+ using ToCacheLatestViewT = std::ranges::cache_latest_view<NonSizedView>;
+
+ static_assert(!HasSize<ToCacheLatestViewT&>);
+ static_assert(!HasSize<ToCacheLatestViewT&&>);
+ static_assert(!HasSize<const ToCacheLatestViewT&>);
+ static_assert(!HasSize<const ToCacheLatestViewT&&>);
+ }
+ {
+ struct SizedView : std::ranges::view_base {
+ bidirectional_iterator<int*> begin();
+ bidirectional_iterator<int*> end();
+
+ int size() const;
+ };
+
+ using ToCacheLatestViewT = std::ranges::cache_latest_view<SizedView>;
+
+ static_assert(std::ranges::sized_range<ToCacheLatestViewT&>);
+ static_assert(!std::ranges::range<const ToCacheLatestViewT&>); // no begin()/end()
+
+ static_assert(HasSize<ToCacheLatestViewT&>);
+ static_assert(HasSize<ToCacheLatestViewT&&>);
+ static_assert(!HasSize<const ToCacheLatestViewT&>); // not a view, therefore no size()
+ static_assert(!HasSize<const ToCacheLatestViewT&&>);
+ }
+ {
+ // Test an empty view.
+ int arr[] = {94};
+ auto view = std::ranges::cache_latest_view(std::ranges::subrange(arr, arr));
+
+ assert(view.size() == 0);
+ assert(std::as_const(view).size() == 0);
+ }
+ {
+ // Test a non-empty view.
+ int arr[] = {94};
+ auto view = std::ranges::cache_latest_view(std::ranges::subrange(arr, arr + 1));
+
+ assert(view.size() == 1);
+ assert(std::as_const(view).size() == 1);
+ }
+ {
+ // Test a non-view.
+ std::array<int, 2> arr = {94, 82};
+ auto view = std::ranges::cache_latest_view(std::move(arr));
+
+ assert(view.size() == 2);
+ assert(std::as_const(view).size() == 2);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 09126c14478bc..23ad3f3b2bd48 100644
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -1118,6 +1118,11 @@ def add_version_header(tc):
"values": {"c++23": 202207},
"headers": ["ranges"],
},
+ {
+ "name": "__cpp_lib_ranges_cache_latest",
+ "values": {"c++26": 202411},
+ "headers": ["ranges"],
+ },
{
"name": "__cpp_lib_ranges_chunk",
"values": {"c++23": 202202},
>From e0542b398e8d1261bf997477653801eeafe6af29 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Wed, 2 Sep 2026 12:15:17 +0200
Subject: [PATCH 02/15] update: reserve_hint to use proper libcxx attributes
---
libcxx/include/__ranges/cache_latest_view.h | 23 ++++++++++++---------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index fcf6b22e364a2..43e1bd1280e6a 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -90,16 +90,19 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
}
// TODO: Implement when P2846R6 is available.
- // constexpr auto reserve_hint()
- // requires approximately_sized_range<_View>
- // {
- // return ranges::reserve_hint(__base_);
- // }
- // constexpr auto reserve_hint() const
- // requires approximately_sized_range<const _View>
- // {
- // return ranges::reserve_hint(__base_);
- // }
+ // # if _LIBCPP_STD_VER >= 26
+
+ // [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
+ // requires approximately_sized_range<_View>
+ // {
+ // return ranges::reserve_hint(__base_);
+ // }
+
+ // [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint() const
+ // requires approximately_sized_range<const _View>
+ // {
+ // return ranges::reserve_hint(__base_);
+ // }
};
template <class _Range>
>From c1bab9ce11be0638d0cc1cb5d17ff7fb7d727522 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Wed, 2 Sep 2026 12:16:54 +0200
Subject: [PATCH 03/15] typo: missing endif
---
libcxx/include/__ranges/cache_latest_view.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index 43e1bd1280e6a..340dddc78e40e 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -103,6 +103,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
// {
// return ranges::reserve_hint(__base_);
// }
+ // # endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
>From 1d2943d9b77d887e261e69856bae9afeb3cd4144 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Wed, 2 Sep 2026 12:24:00 +0200
Subject: [PATCH 04/15] nodiscard: reserve_hint() muted tests
---
.../range.cache.latest/nodiscard.verify.cpp | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
index 1fa32a0375d07..08207a94ad7b2 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -35,6 +35,15 @@ void test() {
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::as_const(clv).size();
+// TODO: Unmute when P2846R6 is available.
+// #if TEST_STD_VER >= 26
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ // clv.reserve_hint();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ // std::as_const(clv).reserve_hint();
+// #endif
+
// [range.cache.latest.iterator]
auto it = clv.begin();
@@ -66,4 +75,4 @@ void test() {
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::views::cache_latest(range);
-}
\ No newline at end of file
+}
>From 8d6241363d7906b100b423765f95ccd325680864 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Wed, 2 Sep 2026 12:25:50 +0200
Subject: [PATCH 05/15] fix: clang-format
---
libcxx/include/__ranges/cache_latest_view.h | 2 +-
.../range.adaptors/range.cache.latest/nodiscard.verify.cpp | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index 340dddc78e40e..9236697959099 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -103,7 +103,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
// {
// return ranges::reserve_hint(__base_);
// }
- // # endif // _LIBCPP_STD_VER >= 26
+ // # endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
index 08207a94ad7b2..594b0ac00fc96 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -35,14 +35,14 @@ void test() {
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::as_const(clv).size();
-// TODO: Unmute when P2846R6 is available.
-// #if TEST_STD_VER >= 26
+ // TODO: Unmute when P2846R6 is available.
+ // #if TEST_STD_VER >= 26
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
// clv.reserve_hint();
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
// std::as_const(clv).reserve_hint();
-// #endif
+ // #endif
// [range.cache.latest.iterator]
>From da2a423a51e4a358d2b707f0ec3869bbcae8b077 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:17:09 +0200
Subject: [PATCH 06/15] temp: port reserve_hint.h impl from #206385
---
libcxx/include/__ranges/cache_latest_view.h | 28 ++++----
libcxx/include/__ranges/concepts.h | 15 ++++
libcxx/include/__ranges/reserve_hint.h | 80 +++++++++++++++++++++
3 files changed, 109 insertions(+), 14 deletions(-)
create mode 100644 libcxx/include/__ranges/reserve_hint.h
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index 9236697959099..fdc0a8e5e161f 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -90,20 +90,20 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
}
// TODO: Implement when P2846R6 is available.
- // # if _LIBCPP_STD_VER >= 26
-
- // [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
- // requires approximately_sized_range<_View>
- // {
- // return ranges::reserve_hint(__base_);
- // }
-
- // [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint() const
- // requires approximately_sized_range<const _View>
- // {
- // return ranges::reserve_hint(__base_);
- // }
- // # endif // _LIBCPP_STD_VER >= 26
+ # if _LIBCPP_STD_VER >= 26
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
+ requires approximately_sized_range<_View>
+ {
+ return ranges::reserve_hint(__base_);
+ }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint() const
+ requires approximately_sized_range<const _View>
+ {
+ return ranges::reserve_hint(__base_);
+ }
+ endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
diff --git a/libcxx/include/__ranges/concepts.h b/libcxx/include/__ranges/concepts.h
index e1c855860c50a..9dc60f6374066 100644
--- a/libcxx/include/__ranges/concepts.h
+++ b/libcxx/include/__ranges/concepts.h
@@ -25,6 +25,7 @@
#include <__ranges/data.h>
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/enable_view.h>
+#include <__ranges/reserve_hint.h>
#include <__ranges/size.h>
#include <__type_traits/add_pointer.h>
#include <__type_traits/common_reference.h>
@@ -80,10 +81,24 @@ using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<_Rp>>;
template <range _Rp>
using range_common_reference_t = iter_common_reference_t<iterator_t<_Rp>>;
+# if _LIBCPP_STD_VER >= 26
+
+// [range.approximately.sized]
+template <class _Tp>
+concept approximately_sized_range = range<_Tp> && requires(_Tp& __t) { ranges::reserve_hint(__t); };
+
+// [range.sized]
+template <class _Tp>
+concept sized_range = approximately_sized_range<_Tp> && requires(_Tp& __t) { ranges::size(__t); };
+
+# else
+
// [range.sized]
template <class _Tp>
concept sized_range = range<_Tp> && requires(_Tp& __t) { ranges::size(__t); };
+# endif // _LIBCPP_STD_VER >= 26
+
template <sized_range _Rp>
using range_size_t = decltype(ranges::size(std::declval<_Rp&>()));
diff --git a/libcxx/include/__ranges/reserve_hint.h b/libcxx/include/__ranges/reserve_hint.h
new file mode 100644
index 0000000000000..c523caf19e77c
--- /dev/null
+++ b/libcxx/include/__ranges/reserve_hint.h
@@ -0,0 +1,80 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RANGES_RESERVE_HINT_H
+#define _LIBCPP___RANGES_RESERVE_HINT_H
+
+#include <__concepts/class_or_enum.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__ranges/size.h>
+#include <__type_traits/remove_cvref.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 26
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// [range.prim.size.hint]
+
+namespace ranges {
+namespace __reserve_hint {
+void reserve_hint() = delete;
+
+template <typename _Tp>
+concept __sized = requires(_Tp&& __t) { ranges::size(__t); };
+
+template <typename _Tp>
+concept __member_reserve_hint = !__sized<_Tp> && requires(_Tp&& __t) {
+ { auto(__t.reserve_hint()) } -> __integer_like;
+};
+
+template <typename _Tp>
+concept __unqualified_reserve_hint =
+ !__sized<_Tp> && !__member_reserve_hint<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
+ { auto(reserve_hint(__t)) } -> __integer_like;
+ };
+
+struct __fn {
+ // `[range.prim.size.hint]`: `ranges::size(t)` is a valid expression
+ template <__sized _Tp>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr __integer_like auto
+ operator()(_Tp&& __t) noexcept(noexcept(ranges::size(__t))) {
+ return ranges::size(__t);
+ }
+
+ // `[range.prim.size.hint]`: `auto(t.reserve_hint())` is a valid expression
+ template <__member_reserve_hint _Tp>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr __integer_like auto
+ operator()(_Tp&& __t) noexcept(noexcept(auto(__t.reserve_hint()))) {
+ return auto(__t.reserve_hint());
+ }
+
+ // `[range.prim.size.hint]`: `auto(reserve_hint(t))` is a valid expression
+ template <__unqualified_reserve_hint _Tp>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr __integer_like auto
+ operator()(_Tp&& __t) noexcept(noexcept(auto(reserve_hint(__t)))) {
+ return auto(reserve_hint(__t));
+ }
+};
+} // namespace __reserve_hint
+
+inline namespace __cpo {
+inline constexpr auto reserve_hint = __reserve_hint::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 26
+
+#endif // _LIBCPP___RANGES_RESERVE_HINT_H
>From c5b1991cc61afda9d4f4098b87c1f365c141fd17 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:23:18 +0200
Subject: [PATCH 07/15] update: nodiscard test with reserve_hint
---
.../range.cache.latest/nodiscard.verify.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
index 594b0ac00fc96..4ae5409e50cd0 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -36,13 +36,15 @@ void test() {
std::as_const(clv).size();
// TODO: Unmute when P2846R6 is available.
- // #if TEST_STD_VER >= 26
+#if TEST_STD_VER >= 26
+
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
- // clv.reserve_hint();
+ clv.reserve_hint();
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
- // std::as_const(clv).reserve_hint();
- // #endif
+ std::as_const(clv).reserve_hint();
+
+#endif
// [range.cache.latest.iterator]
>From edd0e5bb0b2328ca8aff19fb2eb6ce8709986963 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:27:01 +0200
Subject: [PATCH 08/15] fix: missing include
---
libcxx/include/__ranges/cache_latest_view.h | 5 +++--
.../range.adaptors/range.cache.latest/nodiscard.verify.cpp | 1 -
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index fdc0a8e5e161f..704809ec70d13 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -22,6 +22,7 @@
#include <__ranges/concepts.h>
#include <__ranges/non_propagating_cache.h>
#include <__ranges/range_adaptor.h>
+#include <__ranges/reserve_hint.h>
#include <__ranges/size.h>
#include <__ranges/view_interface.h>
#include <__type_traits/add_pointer.h>
@@ -89,7 +90,6 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::size(__base_);
}
- // TODO: Implement when P2846R6 is available.
# if _LIBCPP_STD_VER >= 26
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
@@ -103,7 +103,8 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
{
return ranges::reserve_hint(__base_);
}
- endif // _LIBCPP_STD_VER >= 26
+
+ endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
index 4ae5409e50cd0..fcad1939a85fb 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -35,7 +35,6 @@ void test() {
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
std::as_const(clv).size();
- // TODO: Unmute when P2846R6 is available.
#if TEST_STD_VER >= 26
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
>From e7a4dcbee0e1e2ccf62a915f28ace0d63315865a Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:33:21 +0200
Subject: [PATCH 09/15] fix: clang-format
---
libcxx/include/__ranges/cache_latest_view.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index 704809ec70d13..cb91058ab5a2e 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -90,7 +90,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::size(__base_);
}
- # if _LIBCPP_STD_VER >= 26
+ # if _LIBCPP_STD_VER >= 26
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
requires approximately_sized_range<_View>
@@ -104,7 +104,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::reserve_hint(__base_);
}
- endif // _LIBCPP_STD_VER >= 26
+ # endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
>From 78c04ae7848a9e65adf0e632dfe4f1009e25f219 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:37:04 +0200
Subject: [PATCH 10/15] fix: clang-format
---
libcxx/include/__ranges/cache_latest_view.h | 4 ++--
libcxx/include/__ranges/concepts.h | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index cb91058ab5a2e..c631dbace9ff2 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -90,7 +90,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::size(__base_);
}
- # if _LIBCPP_STD_VER >= 26
+# if _LIBCPP_STD_VER >= 26
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
requires approximately_sized_range<_View>
@@ -104,7 +104,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::reserve_hint(__base_);
}
- # endif // _LIBCPP_STD_VER >= 26
+# endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
diff --git a/libcxx/include/__ranges/concepts.h b/libcxx/include/__ranges/concepts.h
index 9dc60f6374066..b25ab4bbd8391 100644
--- a/libcxx/include/__ranges/concepts.h
+++ b/libcxx/include/__ranges/concepts.h
@@ -97,7 +97,7 @@ concept sized_range = approximately_sized_range<_Tp> && requires(_Tp& __t) { ran
template <class _Tp>
concept sized_range = range<_Tp> && requires(_Tp& __t) { ranges::size(__t); };
-# endif // _LIBCPP_STD_VER >= 26
+# endif // _LIBCPP_STD_VER >= 26
template <sized_range _Rp>
using range_size_t = decltype(ranges::size(std::declval<_Rp&>()));
>From 56aeec8cc38a07bf770d287dd091e86a6a3d9fda Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 3 Sep 2026 13:41:48 +0200
Subject: [PATCH 11/15] fix: clang-format
---
libcxx/include/__ranges/cache_latest_view.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__ranges/cache_latest_view.h b/libcxx/include/__ranges/cache_latest_view.h
index c631dbace9ff2..5e65ec6748fd9 100644
--- a/libcxx/include/__ranges/cache_latest_view.h
+++ b/libcxx/include/__ranges/cache_latest_view.h
@@ -90,7 +90,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::size(__base_);
}
-# if _LIBCPP_STD_VER >= 26
+# if _LIBCPP_STD_VER >= 26
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto reserve_hint()
requires approximately_sized_range<_View>
@@ -104,7 +104,7 @@ class cache_latest_view : public view_interface<cache_latest_view<_View>> {
return ranges::reserve_hint(__base_);
}
-# endif // _LIBCPP_STD_VER >= 26
+# endif // _LIBCPP_STD_VER >= 26
};
template <class _Range>
>From 3c309605fad762867c316e1413f16d53a7d4df95 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Fri, 4 Sep 2026 13:12:39 +0300
Subject: [PATCH 12/15] Fix CI
---
libcxx/include/CMakeLists.txt | 1 +
.../support.limits.general/ranges.version.compile.pass.cpp | 7 +++++++
.../version.version.compile.pass.cpp | 7 +++++++
3 files changed, 15 insertions(+)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 9270af817c53a..e37665c3bf024 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -785,6 +785,7 @@ set(files
__ranges/ref_view.h
__ranges/rend.h
__ranges/repeat_view.h
+ __ranges/reserve_hint.h
__ranges/reverse_view.h
__ranges/single_view.h
__ranges/size.h
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
index 4f2d3e180fd28..0c53b6901b57e 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/ranges.version.compile.pass.cpp
@@ -609,6 +609,13 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++29"
# endif
+# ifndef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should be defined in c++29"
+# endif
+# if __cpp_lib_ranges_cache_latest != 202411L
+# error "__cpp_lib_ranges_cache_latest should have the value 202411L in c++29"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++29"
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 ee4f2406d8e25..d710a479a25d7 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
@@ -10136,6 +10136,13 @@
# error "__cpp_lib_ranges_as_rvalue should have the value 202207L in c++29"
# endif
+# ifndef __cpp_lib_ranges_cache_latest
+# error "__cpp_lib_ranges_cache_latest should be defined in c++29"
+# endif
+# if __cpp_lib_ranges_cache_latest != 202411L
+# error "__cpp_lib_ranges_cache_latest should have the value 202411L in c++29"
+# endif
+
# if !defined(_LIBCPP_VERSION)
# ifndef __cpp_lib_ranges_chunk
# error "__cpp_lib_ranges_chunk should be defined in c++29"
>From 8780e9d3ebaf7d0fd759924f612619886a3d966a Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Fri, 4 Sep 2026 17:35:33 +0300
Subject: [PATCH 13/15] Fix CI
---
.../range.adaptors/range.cache.latest/nodiscard.verify.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
index fcad1939a85fb..e56888c33c4b7 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.cache.latest/nodiscard.verify.cpp
@@ -14,6 +14,8 @@
#include <utility>
#include <vector>
+#include "test_macros.h"
+
void test() {
std::vector<int> range;
std::ranges::cache_latest_view clv{range};
>From 602b5da4c7c9d1ff544f8f5779f483e8503e6065 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Fri, 4 Sep 2026 18:20:27 +0300
Subject: [PATCH 14/15] Fix CI
---
libcxx/include/module.modulemap.in | 1 +
libcxx/modules/std/ranges.inc | 9 +++++++++
2 files changed, 10 insertions(+)
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 068ea8e730b06..a2c239a06d924 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -2005,6 +2005,7 @@ module std {
module ref_view { header "__ranges/ref_view.h" }
module rend { header "__ranges/rend.h" }
module repeat_view { header "__ranges/repeat_view.h" }
+ module reserve_hint { header "__ranges/reserve_hint.h" }
module reverse_view { header "__ranges/reverse_view.h" }
module single_view { header "__ranges/single_view.h" }
module size { header "__ranges/size.h" }
diff --git a/libcxx/modules/std/ranges.inc b/libcxx/modules/std/ranges.inc
index 0434fbc86a829..356545db0d3ea 100644
--- a/libcxx/modules/std/ranges.inc
+++ b/libcxx/modules/std/ranges.inc
@@ -25,6 +25,10 @@ export namespace std {
using std::ranges::__cpo::empty;
using std::ranges::__cpo::size;
using std::ranges::__cpo::ssize;
+
+#if _LIBCPP_STD_VER >= 26
+ using std::ranges::__cpo::reserve_hint;
+#endif
} // namespace __cpo
// [range.range], ranges
@@ -50,6 +54,11 @@ export namespace std {
using std::ranges::disable_sized_range;
using std::ranges::sized_range;
+#if _LIBCPP_STD_VER >= 26
+ // [range.approximately.sized], approximately sized ranges
+ using std::ranges::approximately_sized_range;
+#endif
+
// [range.view], views
using std::ranges::enable_view;
using std::ranges::view;
>From bb1220d14b7935710e9511f19112cb6f84b362b3 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Fri, 4 Sep 2026 19:20:41 +0300
Subject: [PATCH 15/15] Disable ADL
---
libcxx/include/__ranges/reserve_hint.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/include/__ranges/reserve_hint.h b/libcxx/include/__ranges/reserve_hint.h
index c523caf19e77c..72ab7bd5c6993 100644
--- a/libcxx/include/__ranges/reserve_hint.h
+++ b/libcxx/include/__ranges/reserve_hint.h
@@ -63,6 +63,7 @@ struct __fn {
template <__unqualified_reserve_hint _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr __integer_like auto
operator()(_Tp&& __t) noexcept(noexcept(auto(reserve_hint(__t)))) {
+ // NOLINTNEXTLINE(bugprone-unhandled-self-assignment) -- ADL is intentional here
return auto(reserve_hint(__t));
}
};
More information about the libcxx-commits
mailing list