From cppreference.com
Defined in header
template<ranges::viewV>requiresranges::input_range<V>classas_const_view:publicranges::view_interface<as_const_view<V>> (1) (since C++23)namespaceviews{inlineconstexpr/* unspecified */as_const=/* unspecified */;} (2) (since C++23)Call signature
template<ranges::viewable_rangeR>requires/* see below */constexprranges::viewautoas_const(R&&r);(since C++23)1) A range adaptor that represents a view of underlying
that is also a
. An as_const_view always has read-only elements (if not empty).
2)
. 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
to:
views::all(e), if it is a well-formed expression and views::all_t<T> models
;
otherwise, std::span<constX,Extent>(e) for some type X and some extent Extent if U denotes std::span<X,Extent>;
otherwise, ranges::ref_view(static_cast<constX&>(e.base())) if U denotes ranges::ref_view<X> for some type X and constX models
;
otherwise, ranges::ref_view(static_cast<constU&>(e)) if e is an lvalue, constU models
, and U does not model
.
otherwise, as_const_view{e}.
as_const_view always models
, and it models the
,
,
,
,
,
, and
when the underlying view V models respective concepts.
Data members
Member Description Vbase_(private)the underlying view
(exposition-only member object*)Member functions
constructs an as_const_view
(public member function)
returns the underlying view V
(public member function)
returns the beginning iterator of the as_const_view
(public member function)
returns the end iterator of the as_const_view
(public member function)
returns the size of the view if it is bounded
(public member function)
(C++26)
returns the approximate size of the underlying
(public member function) Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
gets the address of derived view's data, provided only if its iterator type satisfies
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
std::ranges::as_const_view::as_const_view
as_const_view()requiresstd::default_initializable<V>=default; (1) (since C++23)constexprexplicitas_const_view(Vbase); (2) (since C++23)1) Value-initializes
via its default member initializer (=V()).
2) Initializes
with std::move(base).
Parameters
base - a view std::ranges::as_const_view::base
constexprVbase()const&requiresstd::copy_constructible<V>; (1) (since C++23)constexprVbase()&&; (2) (since C++23)Returns the underlying view.
1) Copy-constructs the result from the underlying view. Equivalent to return
;.
2) Move-constructs the result from the underlying view. Equivalent to returnstd::move(
);.
std::ranges::as_const_view::begin
constexprautobegin()requires(!/*simple_view*/<V>); (1) (since C++23)constexprautobegin()constrequiresranges::range<constV>; (2) (since C++23)Returns the constant iterator of the view. Equivalent to returnranges::cbegin(
);.
std::ranges::as_const_view::end
constexprautoend()requires(!/*simple_view*/<V>); (1) (since C++23)constexprautoend()constrequiresranges::range<constV>; (2) (since C++23)Returns the constant sentinel of the view. Equivalent to returnranges::cend(
);.
std::ranges::as_const_view::size
constexprautosize()requiresranges::sized_range<V>; (1) (since C++23)constexprautosize()constrequiresranges::sized_range<constV>; (2) (since C++23)Returns the size of the view if the view is bounded. Equivalent to returnranges::size(
);.
std::ranges::as_rvalue_view::reserve_hint
constexprautoreserve_hint()requiresranges::approximately_sized_range<V>; (1) (since C++26)constexprautoreserve_hint()constrequiresranges::approximately_sized_range<constV>; (2) (since C++26)Returns ranges::reserve_hint(
).
Deduction guides
template<classR>as_const_view(R&&)->as_const_view<views::all_t<R>>;(since C++23)Helper templates
template<classT>constexprboolenable_borrowed_range<std::ranges::as_const_view<T>>=ranges::enable_borrowed_range<T>;(since C++23)This specialization of
makes as_const_view satisfy
when the underlying view satisfies it.
Notes
macro ValueStdFeature
(C++23)ranges::as_const_view, std::const_iterator
(C++26)ranges::approximately_sized_range and
Example
Run this code
#include<cassert>#include<ranges>intmain(){intx[]{1,2,3,4,5};autov1=x|std::views::drop(2);assert(v1.back()==5);v1[0]++;// OK, can modify non-const elementautov2=x|std::views::drop(2)|std::views::as_const;assert(v2.back()==5);// v2[0]++; // Compile-time error, cannot modify read-only element}See also