std::ranges...s_const_view(3)
π NAME π
Okay bestie, welcome to the std::ranges...s_const_view(3) page from the C++ Standard Libary π. The whole vibe is about std::ranges::views::as_const,std::ranges::as_const_view β yes, that comma is living its best life exactly like that, no notes.
std::ranges::views::as_const,std::ranges::as_const_view - std::ranges::views::as_const,std::ranges::as_const_view
β¨ Synopsis β¨
Defined in header <ranges> π
template< ranges::view V >
requires ranges::input_range<V>
(1) (since C++23)
class as_const_view
: public ranges::view_interface<as_const_view<V>>
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)
constexpr ranges::view auto as_const( R&& r );
- (1) A range adaptor that represents a view of underlying view that is also a
constant_range. Anas_const_viewalways has read-only elements (if not empty). π - (2)
RangeAdaptorObject. Letebe a subexpression, letTbedecltype((e)), and letUbestd::remove_cvref_t<T>. Then the expressionviews::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 typeXand some extentExtentifUdenotesstd::span<X, Extent>; - otherwise,
ranges::ref_view(static_cast<const X&>(e.base()))ifUdenotesranges::ref_view<X>for some typeXandconst Xmodelsconstant_range; - otherwise,
ranges::ref_view(static_cast<const U&>(e))ifeis an lvalue,const Umodelsconstant_range, andUdoes 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. Slay π
π Member functions π
constructorβ constructs anas_const_view(public member function)baseβ returns the underlying viewV(public member function)beginβ returns the beginning iterator of theas_const_view(public member function)endβ returns the end iterator of theas_const_view(public member function)sizeβ returns the size of the view if it is bounded (public member function)
π Inherited from std::ranges::view_interface π
empty(C++20)β returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (public member function ofstd::ranges::view_interface<D>)cbegin(C++23)β returns a constant iterator to the beginning of the range. (public member function ofstd::ranges::view_interface<D>)cend(C++23)β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20)β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)data(C++20)β gets the address of derived view's data. Provided if its iterator type satisfiescontiguous_iterator. (public member function ofstd::ranges::view_interface<D>)front(C++20)β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20)β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::ranges::view_interface<D>)operator[](C++20)β returns the n^th element in the derived view. Provided if it satisfiesrandom_access_range. (public member function ofstd::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)
- Value-initializes
base_via its default member initializer (= V()). - Initializes
base_withstd::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.
- Copy-constructs the result from the underlying view. Equivalent to
return base_;. - Move-constructs the result from the underlying view. Equivalent to
return std::move(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.
1,2) Equivalent to return ranges::cbegin(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.
1,2) Equivalent to return ranges::cend(base_);
β 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.
1,2) Equivalent to return ranges::size(base_);
π§ Deduction guides π§
template< class R > (since C++23)
as_const_view( R&& ) -> as_const_view<views::all_t<R>>;
π Helper templates π
template< class T >
inline constexpr bool
enable_borrowed_range<std::ranges::as_const_view<T>> (since
=
C++23)
std::ranges::enable_borrowed_range<T>;
This specialization of std::ranges::enable_borrowed_range makes as_const_view satisfy borrowed_range when the underlying view satisfies it. Bestie, that's the whole point π
π Notes π
Feature-test macro realness:
- Feature-test macro:
__cpp_lib_ranges_as_const - Value:
202207L - Std:
(C++23) - Feature:
std::ranges::as_const_view,std::const_iterator
π₯ Example π₯
// Run this 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 π
ranges::as_rvalue_viewβ a view of a sequence that casts each element to an rvalue;views::as_rvalueβ(class template)(range adaptor object);(C++23)ranges::cbeginβ returns an iterator to the beginning of a read-only range;(customization point object);(C++20)ranges::cendβ returns a sentinel indicating the end of a read-only range;(customization point object);(C++20)as_constβ obtains a reference to const to its argument;(function template);(C++17)basic_const_iteratorβ iterator adaptor that converts an iterator into a constant iterator;(class template);(C++23)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, and the page is std::ranges...s_const_view(3). Periodt. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms