26 Ranges library [ranges]

26.7 Range adaptors [range.adaptors]

26.7.21 As const view [range.as.const] Overview [range.as.const.overview]

as_­const_­view presents a view of an underlying sequence as constant.
That is, the elements of an as_­const_­view cannot be modified.
The name views​::​as_­const denotes a range adaptor object ([range.adaptor.object]).
Let E be an expression, let T be decltype((E)), and let U be remove_­cvref_­t<T>.
The expression views​::​as_­const(E) is expression-equivalent to:
  • If views​::​all_­t<T> models constant_­range, then views​::​all(E).
  • Otherwise, if U denotes span<X, Extent> for some type X and some extent Extent, then span<const X, Extent>(E).
  • Otherwise, if E is an lvalue, const U models constant_­range, and U does not model view, then ref_­view(static_­cast<const U&>(E)).
  • Otherwise, as_­const_­view(E).
[Example 1: template<ranges::constant_­range R> void cant_touch_this(R&&); vector<char> hammer = {'m', 'c'}; span<char> beat = hammer; cant_touch_this(views::as_const(beat)); // will not modify the elements of hammer — end example] Class template as_­const_­view [range.as.const.view]

namespace std::ranges { template<view V> requires input_­range<V> class as_const_view : public view_interface<as_const_view<V>> { V base_ = V(); // exposition only public: as_const_view() requires default_­initializable<V> = default; constexpr explicit as_const_view(V base); constexpr V base() const & requires copy_­constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr auto begin() requires (!simple-view<V>) { return ranges::cbegin(base_); } constexpr auto begin() const requires range<const V> { return ranges::cbegin(base_); } constexpr auto end() requires (!simple-view<V>) { return ranges::cend(base_); } constexpr auto end() const requires range<const V> { return ranges::cend(base_); } constexpr auto size() requires sized_­range<V> { return ranges::size(base_); } constexpr auto size() const requires sized_­range<const V> { return ranges::size(base_); } }; template<class R> as_const_view(R&&) -> as_const_view<views::all_t<R>>; }
constexpr explicit as_const_view(V base);
Effects: Initializes base_­ with std​::​move(base).