Namespaces
Variants

std::ranges::cache_latest_view<V>::iterator

From cppreference.com
 
 
Ranges library
Range adaptors
 
 
struct /*iterator*/;
(since C++26)
(exposition only*)

The return type of std::ranges::cache_latest_view<V>::begin().

iterator is an input_iterator, but does not satisfy LegacyInputIterator, and thus does not work with pre-C++20 algorithms.

Nested types

Iterator property types
Type Definition
difference_type ranges::range_difference_t<V>
value_type ranges::range_value_t<V>
iterator_concept std::input_iterator_tag

Data members

Member Description
ranges::iterator_t<V> current_ an iterator into the underlying view
(exposition-only member object*)
cache_latest_view* parent_ a pointer to the parent object
(exposition-only member object*)

Member functions

constructs the iterator
(public member function)
returns the underlying iterator
(public member function)
forwards to the underlying iterator
(public member function)
advances the iterator
(public member function)

std::ranges::cache_latest_view::iterator::iterator

/*iterator*/( /*iterator*/&& ) = default;
(1) (since C++26)
constexpr explicit /*iterator*/( cache_latest_view& parent );
(2) (since C++26)
(exposition only*)
1) The move constructor is defaulted.
2) Initializes current_ with ranges::begin(parent./*base_*/) and parent_ with std::addressof(parent). This constructor is inaccessible to the users.

std::ranges::cache_latest_view::iterator::base

constexpr ranges::iterator_t<V> base() &&;
(1) (since C++26)
constexpr const ranges::iterator_t<V>& base() const & noexcept;
(2) (since C++26)
1) Returns std::move(/*current_*/).
2) Returns current_.

std::ranges::cache_latest_view::iterator::operator*

ranges::range_reference_t<V>& operator*() const;
(since C++26)
Equivalent to
if constexpr (std::is_reference_v<ranges::range_reference_t<V>>)
{
    if (!/*parent_*/->/*cache_*/)
      /*parent_*/->/*cache_*/ = std::addressof(/*as-lvalue*/(*/*current_*/));
    return **/*parent_*/->/*cache_*/;
}
else
{
    if (!/*parent_*/->/*cache_*/)
        /*parent_*/->/*cache_*/./*emplace-deref*/(/*current_*/);
    return */*parent_*/->/*cache_*/;
}

std::ranges::cache_latest_view::iterator::operator++

constexpr /*iterator*/& operator++();
(1) (since C++26)
constexpr void operator++( int );
(2) (since C++26)
1) Equivalent to
   /*parent_*/->/*cache_*/.reset();
   ++/*current_*/;
   return *this;
2) Equivalent to ++*this.

Non-member functions

(C++26)
casts the result of dereferencing the underlying iterator to its associated rvalue reference type
(function)
(C++26)
swaps the objects pointed to by two underlying iterators
(function)

iter_move(std::ranges::cache_latest_view::iterator)

friend constexpr ranges::range_rvalue_reference_t<V>
    iter_move( const /*iterator*/& i )
        noexcept(noexcept(ranges::iter_move(i./*current_*/)));
(since C++26)

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

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::ranges::cache_latest_view::iterator is an associated class of the arguments.

iter_swap(std::ranges::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 std::indirectly_swappable<ranges::iterator_t<V>>;
(since C++26)

Equivalent to ranges::iter_swap(x./*current_*/, y./*current_*/).

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::ranges::cache_latest_view::iterator is an associated class of the arguments.

Example

#include <iostream>
#include <ranges>
#include <string>
#include <vector>

// An expensive function (e.g., heavy math or network I/O)
std::string fetch_profile_pic(const std::string& name)
{
    std::cout << "[Computing] Fetching pic for " << name << '\n';
    return "image_data_of_" + name;
}

int main()
{
    std::vector<std::string> names{"Alice", "Bob"};

    std::cout << "Pipeline without cache_latest.\n";
    auto pipeline1
        = names
        | std::views::transform(fetch_profile_pic)
        | std::views::filter([](const std::string& img)
        {
            return !img.empty(); // First dereference (predicate check)
        });

    for (const auto& img : pipeline1) // Second dereference (loop body access)
        std::cout << "[Using] " << img << '\n';

    std::cout << "\nPipeline with cache_latest.\n";
    auto pipeline2
        = names
        | std::views::transform(fetch_profile_pic)
        | std::views::cache_latest // Caches the result of iterator's operator*
        | std::views::filter([](const std::string& img)
        {
            return !img.empty(); // Evaluates cache (Fast!)
        });

    for (const auto& img : pipeline2)
        std::cout << "[Using] " << img << '\n'; // Reuses cache (Fast!)
}

Output:

Pipeline without cache_latest.
[Computing] Fetching pic for Alice
[Computing] Fetching pic for Alice
[Using] image_data_of_Alice
[Computing] Fetching pic for Bob
[Computing] Fetching pic for Bob
[Using] image_data_of_Bob

Pipeline with cache_latest.
[Computing] Fetching pic for Alice
[Using] image_data_of_Alice
[Computing] Fetching pic for Bob
[Using] image_data_of_Bob