On this page
std::ranges::views::as_const, std::ranges::as_const_view
Defined in header <ranges> |
||
---|---|---|
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Call signature | ||
|
(since C++23) |
view
that is also a constant_range
. An as_const_view
always has read-only elements (if not empty).
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:
views::all(e)
, if it is a well-formed expression andviews::all_t<T>
modelsconstant_range
;- otherwise,
std::span<const X, Extent>(e)
for some typeX
and some extentExtent
ifU
denotesstd::span<X, Extent>
; - otherwise,
ranges::ref_view(static_cast<const X&>(e.base()))
ifU
denotesranges::ref_view<X>
for some typeX
andconst X
modelsconstant_range
; - otherwise,
ranges::ref_view(static_cast<const U&>(e))
ife
is an lvalue,const U
modelsconstant_range
, andU
does not modelview
. - otherwise,
as_const_view{e}
.
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.
Data members
Member object | Definition |
---|---|
base_ (private) |
The underlying view of type V .(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) |
|
Inherited from |
|
(C++20)
|
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range . (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> ) |
(C++20)
|
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
gets the address of derived view's data. Provided if its iterator type satisfies contiguous_iterator . (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range . (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the nth element in the derived view. Provided if it satisfies random_access_range . (public member function of std::ranges::view_interface<D> ) |
std::ranges::as_const_view::as_const_view
|
(1) | (since C++23) |
|
(2) | (since C++23) |
base_
via its default member initializer (= V()
).
base_
with std::move(base)
.
Parameters
base | - | a view |
std::ranges::as_const_view::base
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Returns the underlying view.
return base_;
.
return std::move(base_);
.
std::ranges::as_const_view::begin
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Returns the constant iterator of the view.
return ranges::cbegin(base_);
std::ranges::as_const_view::end
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Returns the constant sentinel of the view.
return ranges::cend(base_);
std::ranges::as_const_view::size
|
(1) | (since C++23) |
|
(2) | (since C++23) |
Returns the size of the view if the view is bounded.
return ranges::size(base_);
Deduction guides
|
(since C++23) |
Helper templates
|
(since C++23) |
This specialization of std::ranges::enable_borrowed_range
makes as_const_view
satisfy borrowed_range
when the underlying view satisfies it.
Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_as_const |
202207L | (C++23) | std::ranges::as_const_view , std::const_iterator |
Example
#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
}
See also
(C++23)
|
a view of a sequence that casts each element to an rvalue(class template) (range adaptor object) |
(C++20)
|
returns an iterator to the beginning of a read-only range (customization point object) |
(C++20)
|
returns a sentinel indicating the end of a read-only range (customization point object) |
(C++17)
|
obtains a reference to const to its argument (function template) |
(C++23)
|
iterator adaptor that converts an iterator into a constant iterator (class template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/as_const_view