Difference between revisions of "cpp/ranges/as const view"
m (→See also: +as_rvalue_view) |
m (→Notes: lc => c/core as a temporary measure to generate link.) |
||
(12 intermediate revisions by 3 users not shown) | |||
Line 24: | Line 24: | ||
@1@ A range adaptor that represents a view of underlying {{lconcept|view}} that is also a {{lconcept|constant_range}}. An {{tt|as_const_view}} always has read-only elements (if not empty). | @1@ A range adaptor that represents a view of underlying {{lconcept|view}} that is also a {{lconcept|constant_range}}. An {{tt|as_const_view}} always has read-only elements (if not empty). | ||
− | @2@ {{named req|RangeAdaptorObject}}. Let {{c|e}} be a subexpression, let {{c|T}} be {{c|decltype((e))}}, and let {{c|U}} be {{c|std::remove_cvref_t<T>}}. Then the expression {{c|views::as_const(e)}} is | + | @2@ {{named req|RangeAdaptorObject}}. Let {{c|e}} be a subexpression, let {{c|T}} be {{c|decltype((e))}}, and let {{c|U}} be {{c|std::remove_cvref_t<T>}}. Then the expression {{c|views::as_const(e)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to: |
* {{c|views::all(e)}}, if it is a well-formed expression and {{c|views::all_t<T>}} models {{lconcept|constant_range}}; | * {{c|views::all(e)}}, if it is a well-formed expression and {{c|views::all_t<T>}} models {{lconcept|constant_range}}; | ||
* otherwise, {{c|std::span<const X, Extent>(e)}} for some type {{tt|X}} and some extent {{tt|Extent}} if {{tt|U}} denotes {{c|std::span<X, Extent>}}; | * otherwise, {{c|std::span<const X, Extent>(e)}} for some type {{tt|X}} and some extent {{tt|Extent}} if {{tt|U}} denotes {{c|std::span<X, Extent>}}; | ||
− | * otherwise, {{c|ranges::ref_view(static_cast<const U&>(e))}} if {{tt|e}} is an lvalue, {{c|const U}} models {{lconcept|constant_range}}, and {{ | + | * otherwise, {{c|ranges::ref_view(static_cast<const X&>(e.base()))}} if {{tt|U}} denotes {{c|ranges::ref_view<X>}} for some type {{tt|X}} and {{c|const X}} models {{lconcept|constant_range}}; |
+ | * otherwise, {{c|ranges::ref_view(static_cast<const U&>(e))}} if {{tt|e}} is an lvalue, {{c|const U}} models {{lconcept|constant_range}}, and {{tt|U}} does not model {{lconcept|view}}. | ||
* otherwise, {{c|as_const_view{e} }}. | * otherwise, {{c|as_const_view{e} }}. | ||
{{tt|as_const_view}} always models {{lconcept|constant_range}}, and it models the {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}}, {{lconcept|borrowed_range}}, {{lconcept|common_range}}, and {{lconcept|sized_range}} when the underlying view {{tt|V}} models respective concepts. | {{tt|as_const_view}} always models {{lconcept|constant_range}}, and it models the {{lconcept|contiguous_range}}, {{lconcept|random_access_range}}, {{lconcept|bidirectional_range}}, {{lconcept|forward_range}}, {{lconcept|borrowed_range}}, {{lconcept|common_range}}, and {{lconcept|sized_range}} when the underlying view {{tt|V}} models respective concepts. | ||
− | |||
− | |||
===Data members=== | ===Data members=== | ||
− | + | {{dsc begin}} | |
− | + | {{dsc hitem|Member object|Definition}} | |
+ | {{dsc expos mem obj|base_|private=yes|The underlying view of type {{tt|V}}.}} | ||
+ | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc mem ctor| | + | {{dsc mem ctor|{{PAGENAME}}#ctor|constructs an {{tt|as_const_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|{{PAGENAME}}#base|title=base|returns the underlying view {{tt|V}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|{{PAGENAME}}#begin|title=begin|returns the beginning iterator of the {{tt|as_const_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|{{PAGENAME}}#end|title=end|returns the end iterator of the {{tt|as_const_view}}}} |
− | {{dsc mem fun| | + | {{dsc mem fun|{{PAGENAME}}#size|title=size|returns the size of the view if it is bounded}} |
{{cpp/ranges/view_interface/inherit|embedded=yes|size=yes}} | {{cpp/ranges/view_interface/inherit|embedded=yes|size=yes}} | ||
{{dsc end}} | {{dsc end}} | ||
+ | {{anchor|ctor}} | ||
{{member|{{small|std::ranges::as_const_view::}}as_const_view| | {{member|{{small|std::ranges::as_const_view::}}as_const_view| | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 59: | Line 61: | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Value-initializes {{ | + | @1@ Value-initializes {{tti|base_}} via its default member initializer ({{c|1== V()}}). |
− | @2@ Initializes {{ | + | @2@ Initializes {{tti|base_}} with {{c|std::move(base)}}. |
===Parameters=== | ===Parameters=== | ||
Line 68: | Line 70: | ||
}} | }} | ||
+ | {{anchor|base}} | ||
{{member|{{small|std::ranges::as_const_view::}}base| | {{member|{{small|std::ranges::as_const_view::}}base| | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 83: | Line 86: | ||
}} | }} | ||
+ | {{anchor|begin}} | ||
{{member|{{small|std::ranges::as_const_view::}}begin| | {{member|{{small|std::ranges::as_const_view::}}begin| | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|num=1|since=c++23| | {{dcl|num=1|since=c++23| | ||
− | constexpr auto begin() requires (!__simple_view<V> | + | constexpr auto begin() requires (!__simple_view<V>); |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl|num=2|since=c++23| | {{dcl|num=2|since=c++23| | ||
− | constexpr auto begin() const requires ranges::range<const V> | + | constexpr auto begin() const requires ranges::range<const V>; |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Returns the constant iterator of the view. | Returns the constant iterator of the view. | ||
+ | |||
+ | @1,2@ Equivalent to {{c|return ranges::cbegin(base_);}} | ||
}} | }} | ||
+ | {{anchor|end}} | ||
{{member|{{small|std::ranges::as_const_view::}}end| | {{member|{{small|std::ranges::as_const_view::}}end| | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|num=1|since=c++23| | {{dcl|num=1|since=c++23| | ||
− | constexpr auto end() requires (!__simple_view<V> | + | constexpr auto end() requires (!__simple_view<V>); |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl|num=2|since=c++23| | {{dcl|num=2|since=c++23| | ||
− | constexpr auto end() const requires ranges::range<const V> | + | constexpr auto end() const requires ranges::range<const V>; |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Returns the constant sentinel of the view. | Returns the constant sentinel of the view. | ||
+ | |||
+ | @1,2@ Equivalent to {{c|return ranges::cend(base_);}} | ||
}} | }} | ||
+ | {{anchor|size}} | ||
{{member|{{small|std::ranges::as_const_view::}}size| | {{member|{{small|std::ranges::as_const_view::}}size| | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|num=1|since=c++23| | {{dcl|num=1|since=c++23| | ||
− | constexpr auto size() requires ranges::sized_range<V> | + | constexpr auto size() requires ranges::sized_range<V>; |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl|num=2|since=c++23| | {{dcl|num=2|since=c++23| | ||
− | constexpr auto size() const requires ranges::sized_range<const V> | + | constexpr auto size() const requires ranges::sized_range<const V>; |
− | + | ||
− | + | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Returns the size of the view if the view is bounded. | Returns the size of the view if the view is bounded. | ||
+ | |||
+ | @1,2@ Equivalent to {{c|return ranges::size(base_);}} | ||
}} | }} | ||
Line 140: | Line 140: | ||
{{ddcl|since=c++23|1= | {{ddcl|since=c++23|1= | ||
template< class T > | template< class T > | ||
− | + | constexpr bool enable_borrowed_range<std::ranges::as_const_view<T>> = | |
− | + | ranges::enable_borrowed_range<T>; | |
}} | }} | ||
This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|as_const_view}} satisfy {{lconcept|borrowed_range}} when the underlying view satisfies it. | This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|as_const_view}} satisfy {{lconcept|borrowed_range}} when the underlying view satisfies it. | ||
===Notes=== | ===Notes=== | ||
− | {{feature_test_macro|__cpp_lib_ranges_as_const|value=202207L|std=C++23 | + | {{feature_test_macro|__cpp_lib_ranges_as_const|value=202207L|std=C++23|{{tt|ranges::as_const_view}}, {{c/core|std::const_iterator}}}} |
===Example=== | ===Example=== | ||
− | {{ | + | {{example|code= |
+ | #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=== | ===See also=== | ||
Line 157: | Line 173: | ||
{{dsc inc|cpp/ranges/dsc cend}} | {{dsc inc|cpp/ranges/dsc cend}} | ||
{{dsc inc|cpp/utility/dsc as_const}} | {{dsc inc|cpp/utility/dsc as_const}} | ||
− | |||
{{dsc inc|cpp/iterator/dsc basic_const_iterator}} | {{dsc inc|cpp/iterator/dsc basic_const_iterator}} | ||
− | |||
{{dsc end}} | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 14:47, 19 June 2024
Defined in header <ranges>
|
||
template< ranges::view V > requires ranges::input_range<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 */ |
(since C++23) | |
view
that is also a constant_range
. An as_const_view
always has read-only elements (if not empty).- views::all(e), if it is a well-formed expression and views::all_t<T> models
constant_range
; - otherwise, std::span<const X, Extent>(e) for some type
X
and some extentExtent
ifU
denotes std::span<X, Extent>; - otherwise, ranges::ref_view(static_cast<const X&>(e.base())) if
U
denotes ranges::ref_view<X> for some typeX
and const X modelsconstant_range
; - otherwise, ranges::ref_view(static_cast<const U&>(e)) if
e
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.
Contents |
[edit] Data members
Member object | Definition |
base_ (private)
|
The underlying view of type V .(exposition-only member object*) |
[edit] 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 std::ranges::view_interface | |
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> )
|
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> )
| |
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> )
| |
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
| |
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> )
| |
returns the n th 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
as_const_view() requires std::default_initializable<V> = default; |
(1) | (since C++23) |
constexpr explicit as_const_view( V base ); |
(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
constexpr V base() const& requires std::copy_constructible<V>; |
(1) | (since C++23) |
constexpr V base() &&; |
(2) | (since C++23) |
Returns the underlying view.
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.
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.
std::ranges::as_const_view::size
constexpr auto size() requires ranges::sized_range<V>; |
(1) | (since C++23) |
constexpr auto size() const requires ranges::sized_range<const V>; |
(2) | (since C++23) |
Returns the size of the view if the view is bounded.
[edit] Deduction guides
template< class R > as_const_view( R&& ) -> as_const_view<views::all_t<R>>; |
(since C++23) | |
[edit] Helper templates
template< class T > constexpr bool enable_borrowed_range<std::ranges::as_const_view<T>> = |
(since C++23) | |
This specialization of std::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
|
[edit] 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 }
[edit] See also
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) |