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
ebe 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-equivalentto:
constant_range
;X
and some extent Extent
if U
denotes std::span<X, Extent>;U
denotes ranges::ref_view<X> for some type X
and const X models constant_range
;e
is an lvalue, const U models constant_range
, and U
does not model view
.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.
V
base_
(private) the underlying view
as_const_view
V
as_const_view
as_const_view
approximately_sized_range
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] gets the address of derived view's data, provided only if its iterator type satisfies contiguous_iterator
std::ranges::view_interface<D>
) [edit] returns the first element in the derived view, provided if it satisfies forward_range
std::ranges::view_interface<D>
) [edit] returns the last element in the derived view, provided only if it satisfies bidirectional_range
and common_range
std::ranges::view_interface<D>
) [edit] returns the n
th element in the derived view, provided only if it satisfies random_access_range
std::ranges::view_interface<D>
) [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_
via its default member initializer (
= V()).
2)Initializes
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_
;
.
2)Move-constructs the result from the underlying view. Equivalent to
return std::move(base_
);
.
std::ranges::as_const_view::beginconstexpr auto begin() requires (!/*simple_view*/<V>);
(1) (since C++23) (2) (since C++23)Returns the constant iterator of the view. Equivalent to return ranges::cbegin(base_
);.
constexpr auto end() requires (!/*simple_view*/<V>);
(1) (since C++23) (2) (since C++23)Returns the constant sentinel of the view. Equivalent to return ranges::cend(base_
);.
Returns the size of the view if the view is bounded. Equivalent to return ranges::size(base_
);.
constexpr auto reserve_hint()
requires ranges::approximately_sized_range<V>;
constexpr auto reserve_hint() const
requires ranges::approximately_sized_range<const V>;
Returns ranges::reserve_hint(base_
).
This specialization of ranges::enable_borrowed_range makes as_const_view
satisfy borrowed_range
when the underlying view satisfies it.
#include <cassert> #include <ranges> 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4