Files
2025-10-25 03:02:53 +03:00

5.5 KiB
Raw Permalink Blame History

[range.as.const]

25 Ranges library [ranges]

25.7 Range adaptors [range.adaptors]

25.7.22 As const view [range.as.const]

25.7.22.1 Overview [range.as.const.overview]

1

#

as_const_view presents a view of an underlying sequence as constant.

That is, the elements of an as_const_view cannot be modified.

2

#

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.

The expression views::as_const(E) is expression-equivalent to:

  • (2.1)

    If views::all_t models constant_range, then views::all(E).

  • (2.2)

    Otherwise, if U denotes empty_view for some type X, then auto(views::empty).

  • (2.3)

    Otherwise, if U denotes span<X, Extent> for some type X and some extent Extent, then span<const X, Extent>(E).

  • (2.4)

    Otherwise, if U denotes ref_view for some type X andconst X models constant_range, then ref_view(static_cast<const X&>(E.base())).

  • (2.5)

    Otherwise, if E is an lvalue,const U models constant_range, andU does not model view, then ref_view(static_cast<const U&>(E)).

  • (2.6)

    Otherwise, as_const_view(E).

3

#

[Example 1: template<constant_range R>void cant_touch_this(R&&);

vector hammer = {'m', 'c'}; span beat = hammer; cant_touch_this(views::as_const(beat)); // will not modify the elements of hammer — end example]

25.7.22.2 Class template as_const_view [range.as.const.view]

🔗

namespace std::ranges {template<view V>requires input_rangeclass as_const_view : public view_interface<as_const_view> { V base_ = V(); // exposition onlypublic: as_const_view() requires default_initializable = default; constexpr explicit as_const_view(V base); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr auto begin() requires (simple-view) { return ranges::cbegin(base_); }constexpr auto begin() const requires range { return ranges::cbegin(base_); }constexpr auto end() requires (simple-view) { return ranges::cend(base_); }constexpr auto end() const requires range { return ranges::cend(base_); }constexpr auto size() requires sized_range { return ranges::size(base_); }constexpr auto size() const requires sized_range { return ranges::size(base_); }constexpr auto reserve_hint() requires approximately_sized_range{ return ranges::reserve_hint(base_); }constexpr auto reserve_hint() const requires approximately_sized_range{ return ranges::reserve_hint(base_); }}; template as_const_view(R&&) -> as_const_view<views::all_t>;}

🔗

constexpr explicit as_const_view(V base);

1

#

Effects: Initializes base_ with std::move(base).