[range.cache.latest]

# 25 Ranges library [[ranges]](./#ranges)

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.cache.latest)

### 25.7.34 Cache latest view [range.cache.latest]

#### [25.7.34.1](#overview) Overview [[range.cache.latest.overview]](range.cache.latest.overview)

[1](#overview-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17363)

cache_latest_view caches the last-accessed element of
its underlying sequence
so that the element does not have to be recomputed on repeated access[.](#overview-1.sentence-1)

[*Note [1](#overview-note-1)*:

This is useful if computation of the element to produce is expensive[.](#overview-1.sentence-2)

â *end note*]

[2](#overview-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17371)

The name views​::​cache_latest denotes
a range adaptor object ([[range.adaptor.object]](range.adaptor.object "25.7.2 Range adaptor objects"))[.](#overview-2.sentence-1)

Let E be an expression[.](#overview-2.sentence-2)

The expression views​::​cache_latest(E) is expression-equivalent tocache_latest_view(E)[.](#overview-2.sentence-3)

#### [25.7.34.2](#view) Class template cache_latest_view [[range.cache.latest.view]](range.cache.latest.view)

namespace std::ranges {template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") V>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V>class [cache_latest_view](#lib:cache_latest_view "25.7.34.2 Class template cache_­latest_­view [range.cache.latest.view]") : public view_interface<cache_latest_view<V>> { V *base_* = V(); // *exposition only*using *cache-t* = conditional_t<is_reference_v<range_reference_t<V>>, // *exposition only* add_pointer_t<range_reference_t<V>>,
 range_reference_t<V>>; *non-propagating-cache*<*cache-t*> *cache_*; // *exposition only*// [[range.cache.latest.iterator]](#iterator "25.7.34.3 Class cache_­latest_­view​::​iterator"), class cache_latest_view​::​*iterator*class *iterator*; // *exposition only*// [[range.cache.latest.sentinel]](#sentinel "25.7.34.4 Class cache_­latest_­view​::​sentinel"), class cache_latest_view​::​*sentinel*class *sentinel*; // *exposition only*public: cache_latest_view() requires [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<V> = default; constexpr explicit cache_latest_view(V base); constexpr V base() const & requires [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14 Concept copy_­constructible [concept.copyconstructible]")<V> { return *base_*; }constexpr V base() && { return std::move(*base_*); }constexpr auto begin(); constexpr auto end(); constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V>; constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V>; constexpr auto reserve_hint() requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<V>; constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<const V>; }; template<class R> cache_latest_view(R&&) -> cache_latest_view<views::all_t<R>>;}

[ð](#lib:cache_latest_view,constructor)

`constexpr explicit cache_latest_view(V base);
`

[1](#view-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17425)

*Effects*: Initializes *base_* with std​::​move(base)[.](#view-1.sentence-1)

[ð](#lib:begin,cache_latest_view)

`constexpr auto begin();
`

[2](#view-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17436)

*Effects*: Equivalent to: return *iterator*(*this);

[ð](#lib:end,cache_latest_view)

`constexpr auto end();
`

[3](#view-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17447)

*Effects*: Equivalent to: return *sentinel*(*this);

[ð](#lib:size,cache_latest_view)

`constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V>;
constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V>;
`

[4](#view-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17459)

*Effects*: Equivalent to: return ranges​::​size(*base_*);

[ð](#lib:reserve_hint,cache_latest_view)

`constexpr auto reserve_hint() requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<V>;
constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<const V>;
`

[5](#view-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17471)

*Effects*: Equivalent to: return ranges​::​reserve_hint(*base_*);

#### [25.7.34.3](#iterator) Class cache_latest_view​::​*iterator* [[range.cache.latest.iterator]](range.cache.latest.iterator)

[ð](#lib:cache_latest_view::iiterator)

namespace std::ranges {template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") V>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V>class cache_latest_view<V>::*iterator* { cache_latest_view* *parent_*; // *exposition only* iterator_t<V> *current_*; // *exposition only*constexpr explicit *iterator*(cache_latest_view& parent); // *exposition only*public:using difference_type = range_difference_t<V>; using value_type = range_value_t<V>; using iterator_concept = input_iterator_tag; *iterator*(*iterator*&&) = default; *iterator*& operator=(*iterator*&&) = default; constexpr iterator_t<V> base() &&; constexpr const iterator_t<V>& base() const & noexcept; constexpr range_reference_t<V>& operator*() const; constexpr *iterator*& operator++(); constexpr void operator++(int); friend constexpr range_rvalue_reference_t<V> iter_move(const *iterator*& i)noexcept(noexcept(ranges::iter_move(i.*current_*))); friend constexpr void iter_swap(const *iterator*& x, const *iterator*& y)noexcept(noexcept(ranges::iter_swap(x.*current_*, y.*current_*)))requires [indirectly_swappable](alg.req.ind.swap#concept:indirectly_swappable "24.3.7.4 Concept indirectly_­swappable [alg.req.ind.swap]")<iterator_t<V>>; };}

[ð](#lib:cache_latest_view::iterator,constructor)

`constexpr explicit iterator(cache_latest_view& parent);
`

[1](#iterator-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17521)

*Effects*: Initializes *current_* withranges​::​begin(parent.*base_*) and *parent_* with addressof(parent)[.](#iterator-1.sentence-1)

[ð](#lib:base,cache_latest_view::iterator)

`constexpr iterator_t<V> base() &&;
`

[2](#iterator-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17534)

*Returns*: std​::​move(*current_*)[.](#iterator-2.sentence-1)

[ð](#lib:base,cache_latest_view::iterator_)

`constexpr const iterator_t<V>& base() const & noexcept;
`

[3](#iterator-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17545)

*Returns*: *current_*[.](#iterator-3.sentence-1)

[ð](#lib:operator++,cache_latest_view::iterator)

`constexpr iterator& operator++();
`

[4](#iterator-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17556)

*Effects*: Equivalent to:*parent_*->*cache_*.reset();++*current_*;return *this;

[ð](#lib:operator++,cache_latest_view::iterator_)

`constexpr void operator++(int);
`

[5](#iterator-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17572)

*Effects*: Equivalent to: ++*this[.](#iterator-5.sentence-1)

[ð](#lib:operator*,cache_latest_view::iterator)

`constexpr range_reference_t<V>& operator*() const;
`

[6](#iterator-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17583)

*Effects*: Equivalent to:if constexpr (is_reference_v<range_reference_t<V>>) {if (!*parent_*->*cache_*) {*parent_*->*cache_* = addressof(*as-lvalue*(**current_*)); }return ***parent_*->*cache_*;} else {if (!*parent_*->c*ache_*) {*parent_*->*cache_*.*emplace-deref*(*current_*); }return **parent_*->*cache_*;}

[*Note [1](#iterator-note-1)*:

Evaluations of operator* on the same iterator object
can conflict ([[intro.races]](intro.races "6.10.2.2 Data races"))[.](#iterator-6.sentence-1)

â *end note*]

[ð](#lib:iter_move,cache_latest_view::iterator)

`friend constexpr range_rvalue_reference_t<V> iter_move(const iterator& i)
  noexcept(noexcept(ranges::iter_move(i.current_)));
`

[7](#iterator-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17612)

*Effects*: Equivalent to: return ranges​::​iter_move(i.*current_*);

[ð](#lib:iter_swap,cache_latest_view::iterator)

`friend constexpr void iter_swap(const iterator& x, const iterator& y)
  noexcept(noexcept(ranges::iter_swap(x.current_, y.current_)))
  requires [indirectly_swappable](alg.req.ind.swap#concept:indirectly_swappable "24.3.7.4 Concept indirectly_­swappable [alg.req.ind.swap]")<iterator_t<V>>;
`

[8](#iterator-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17625)

*Effects*: Equivalent toranges​::​iter_swap(x.*current_*, y.*current_*)[.](#iterator-8.sentence-1)

#### [25.7.34.4](#sentinel) Class cache_latest_view​::​*sentinel* [[range.cache.latest.sentinel]](range.cache.latest.sentinel)

[ð](#lib:cache_latest_view::sentinel)

namespace std::ranges {template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") V>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V>class cache_latest_view<V>::*sentinel* { sentinel_t<V> *end_* = sentinel_t<V>(); // *exposition only*constexpr explicit *sentinel*(cache_latest_view& parent); // *exposition only*public:*sentinel*() = default; constexpr sentinel_t<V> base() const; friend constexpr bool operator==(const *iterator*& x, const *sentinel*& y); friend constexpr range_difference_t<V> operator-(const *iterator*& x, const *sentinel*& y)requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>; friend constexpr range_difference_t<V> operator-(const *sentinel*& x, const *iterator*& y)requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>; };}

[ð](#lib:cache_latest_view::sentinel,constructor)

`constexpr explicit sentinel(cache_latest_view& parent);
`

[1](#sentinel-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17664)

*Effects*: Initializes *end_* with ranges​::​end(parent.*base_*)[.](#sentinel-1.sentence-1)

[ð](#lib:base,cache_latest_view::sentinel)

`constexpr sentinel_t<V> base() const;
`

[2](#sentinel-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17675)

*Returns*: *end_*[.](#sentinel-2.sentence-1)

[ð](#lib:operator==,cache_latest_view::iterator)

`friend constexpr bool operator==(const iterator& x, const sentinel& y);
`

[3](#sentinel-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17687)

*Returns*: x.*current_* == y.*end_*[.](#sentinel-3.sentence-1)

[ð](#lib:operator-,cache_latest_view::iterator)

`friend constexpr range_difference_t<V> operator-(const iterator& x, const sentinel& y)
  requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;
`

[4](#sentinel-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17700)

*Returns*: x.*current_* - y.*end_*[.](#sentinel-4.sentence-1)

[ð](#lib:operator-,cache_latest_view::iterator_)

`friend constexpr range_difference_t<V> operator-(const sentinel& x, const iterator& y)
  requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;
`

[5](#sentinel-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L17713)

*Returns*: x.*end_* - y.*current_*[.](#sentinel-5.sentence-1)
