Namespaces
Variants

std::ranges::views::as_input, std::ranges::as_input_view

From cppreference.com
 
 
Ranges library
Range adaptors
 
 
Defined in header <ranges>
template< ranges::input_range V >
    requires ranges::view<V>
class as_input_view
    : public ranges::view_interface<as_input_view<V>>
(1) (since C++26)
namespace views {
    inline constexpr /* unspecified */ as_input = /* unspecified */;
}
(2) (since C++26)
Call signature
template< ranges::viewable_range R >
    requires /* see below */
constexpr ranges::view auto as_input( R&& r );
(since C++26)
1) A range adaptor that represents a view of an underlying view as an input_range-only and not a common_range.
2) RangeAdaptorObject. Let e be a subexpression, and let T be decltype(e). Then the expression views::as_input(e) is expression-equivalent to:
  • views::all(e), if it is a well-formed expression and all of the conditions below are true:
  • as_input_view{e} otherwise.

Consequently, as_input_view never models range concepts that are stronger than input_range. In particular, it does not model all of these:

Additionally, as_input_view does not model common_range. However, it does model borrowed_range, constant_range, and sized_range when the underlying view V models respective concepts.

Data members

Member Description
V base_ (private) the underlying view
(exposition-only member object*)

Member functions

constructs an as_input_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator or a sentinel to the end
(public member function)
returns the number of elements, provided only if the underlying (adapted) range satisfies sized_range
(public member function)
returns the approximate number of elements, provided only if the underlying (adapted) range satisfies approximately_sized_range
(public member function)
Inherited from std::ranges::view_interface
returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>) [edit]
returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
(public member function of std::ranges::view_interface<D>) [edit]

std::ranges::as_input_view::as_input_view

as_input_view() requires std::default_initializable<V> = default;
(1) (since C++26)
constexpr explicit as_input_view( V base );
(2) (since C++26)
1) Value-initializes base_ via its default member initializer (= V()).
2) Initializes base_ with std::move(base).

Parameters

base - a view

std::ranges::as_input_view::base

constexpr V base() const& requires std::copy_constructible<V>;
(1) (since C++26)
constexpr V base() &&;
(2) (since C++26)
1) Copy-constructs the result from the underlying view. Equivalent to return base_;.
2) Move-constructs the result from the underlying view. Equivalent to return std::move(base_);.

std::ranges::as_input_view::begin

constexpr auto begin() requires (!/*simple-view*/<V>);
(1) (since C++26)
constexpr auto begin() const requires ranges::range<const V>;
(2) (since C++26)
1) Equivalent to return /*iterator*/<false>(ranges::begin(base_));
2) Equivalent to return /*iterator*/<true>(ranges::begin(base_));

std::ranges::as_input_view::end

constexpr auto end() requires (!/*simple-view*/<V>);
(1) (since C++26)
constexpr auto end() const requires ranges::range<const V>;
(2) (since C++26)
1,2) Equivalent to return ranges::end(base_);

std::ranges::as_input_view::size

constexpr auto size() requires ranges::sized_range<V>;
(1) (since C++26)
constexpr auto size() const requires ranges::sized_range<const V>;
(2) (since C++26)
1,2) Equivalent to return ranges::size(base_);

std::ranges::as_input_view::reserve_hint

constexpr auto reserve_hint()
    requires ranges::sized_range<V>;
(1) (since C++26)
constexpr auto reserve_hint()
    const requires ranges::sized_range<const V>;
(2) (since C++26)
1,2) Equivalent to return ranges::reserve_hint(base_);

Deduction guides

template< class R >
as_input_view( R&& ) -> as_input_view<views::all_t<R>>;
(since C++26)

Nested classes

the iterator type
(exposition-only member class template*)

Helper templates

template< class T >
constexpr bool enable_borrowed_range<std::ranges::as_input_view<T>> =
    ranges::enable_borrowed_range<T>;
(since C++26)

This specialization of std::ranges::enable_borrowed_range makes as_input_view satisfy borrowed_range when the underlying view satisfies it.

Notes

as_input_view can be useful to avoid the overhead necessary to provide support for the operations needed for greater iterator strength.

Feature-test macro Value Std Feature
__cpp_lib_ranges_as_input 202502L (C++26) std::ranges::as_input_view

Example

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

int main()
{
    std::vector<std::vector<int>> matrix
    {
        {1, 2, 3},
        {4, 5},
        {6, 7, 8, 9}
    };
    
    // Without as_input the join_view iterator is forced to maintain complex
    // internal state to preserve forward-iterator capabilities.
    auto heavy_flattened = matrix
                         | std::views::join
                         ;
    for (std::cout << "Standard flatten: "; int num : heavy_flattened)
        std::cout << num << ' ';
    std::cout << '\n';
    
    // With views::as_input the random-access/forward traits are strip away;
    // views::join now realizes it is processing a single-pass input range
    // and drops all multi-pass caching overhead.
    auto fast_flattened = matrix
                        | std::views::as_input
                        | std::views::join
                        ;
    for (std::cout << "Optimized flatten: "; int num : fast_flattened)
        std::cout << num << ' ';
    std::cout << '\n';
}

Output:

Standard flatten: 1 2 3 4 5 6 7 8 9
Optimized flatten: 1 2 3 4 5 6 7 8 9

See also

specifies a range whose iterator type satisfies input_iterator
(concept) [edit]
converts a view into a common_range
(class template) (range adaptor object)[edit]