std::ranges::views::as_const, std::ranges::as_const_view - cppreference.com (original) (raw)

Defined in header
template< ranges::view V > requires ranges::input_range<V> class as_const_view : public ranges::view_interface<as_const_view<V>> (1) (since C++23)
namespace views { inline constexpr /* unspecified */ as_const = /* unspecified */; } (2) (since C++23)
Call signature
template< ranges::viewable_range R > requires /* see below */ constexpr ranges::view auto as_const( R&& r ); (since C++23)
  1. A range adaptor that represents a view of underlying view that is also a constant_range. An as_const_view always has read-only elements (if not empty).

  2. RangeAdaptorObject. Let e be a subexpression, let T be decltype((e)), and let U be std::remove_cvref_t<T>. Then the expression views::as_const(e) is expression-equivalent to:

as_const_view always models constant_range, and it models the contiguous_range, random_access_range, bidirectional_range, forward_range, borrowed_range, common_range, and sized_range when the underlying view V models respective concepts.

Contents

[edit] Data members

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

[edit] Member functions

(constructor) constructs an as_const_view (public member function)
base returns the underlying view V (public member function)
begin returns the beginning iterator of the as_const_view (public member function)
end returns the end iterator of the as_const_view (public member function)
size returns the size of the view if it is bounded (public member function)
reserve_hint(C++26) returns the approximate size of the underlying approximately_sized_range (public member function)
Inherited from std::ranges::view_interface
empty 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) [edit]
cbegin(C++23) returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit]
cend(C++23) returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit]
operator bool 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) [edit]
data gets the address of derived view's data, provided only if its iterator type satisfies contiguous_iterator (public member function of std::ranges::view_interface) [edit]
front returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit]
back returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit]
operator[] returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit]

std::ranges::as_const_view::as_const_view

| | (1) | (since C++23) | | | --------------------------------------------- | ------------- | ------------- | | constexpr explicit as_const_view( V base ); | (2) | (since C++23) |

  1. Value-initializes _[base](#base)_ via its default member initializer (= V()).

  2. Initializes _[base](#base)_ with std::move(base).

Parameters

std::ranges::as_const_view::base

| | (1) | (since C++23) | | | ---------------------- | ------------- | ------------- | | constexpr V base() &&; | (2) | (since C++23) |

Returns the underlying view.

  1. Copy-constructs the result from the underlying view. Equivalent to return _[base](#base)_ ;.

  2. Move-constructs the result from the underlying view. Equivalent to return std::move(_[base](#base)_ );.

std::ranges::as_const_view::begin

constexpr auto begin() requires (!/*simple_view*/<V>); (1) (since C++23)
constexpr auto begin() const requires ranges::range<const V>; (2) (since C++23)

Returns the constant iterator of the view. Equivalent to return ranges::cbegin(_[base](#base)_ );.

std::ranges::as_const_view::end

constexpr auto end() requires (!/*simple_view*/<V>); (1) (since C++23)
constexpr auto end() const requires ranges::range<const V>; (2) (since C++23)

Returns the constant sentinel of the view. Equivalent to return ranges::cend(_[base](#base)_ );.

std::ranges::as_const_view::size

Returns the size of the view if the view is bounded. Equivalent to return ranges::size(_[base](#base)_ );.

std::ranges::as_rvalue_view::reserve_hint

constexpr auto reserve_hint() requires ranges::approximately_sized_range<V>; (1) (since C++26)
constexpr auto reserve_hint() const requires ranges::approximately_sized_range<const V>; (2) (since C++26)

Returns ranges::reserve_hint(_[base](#base)_ ).

[edit] Deduction guides

| template< class R >as_const_view( R&& ) -> as_const_view<views::all_t<R>>; | | (since C++23) | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ------------- |

[edit] Helper templates

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

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_ranges_as_const 202207L (C++23) ranges::as_const_view, std::const_iterator
__cpp_lib_ranges_reserve_hint 202502L (C++26) ranges::approximately_sized_range and reserve_hint

[edit] Example

#include #include   int main() { int x[]{1, 2, 3, 4, 5};   auto v1 = x | std::views::drop(2); assert(v1.back() == 5); v1[0]++; // OK, can modify non-const element   auto v2 = x | std::views::drop(2) | std::views::as_const; assert(v2.back() == 5); // v2[0]++; // Compile-time error, cannot modify read-only element }

[edit] See also