[range.as.const]

# 25 Ranges library [[ranges]](./#ranges)

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.as.const)

### 25.7.22 As const view [range.as.const]

#### [25.7.22.1](#overview) Overview [[range.as.const.overview]](range.as.const.overview)

[1](#overview-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L10028)

as_const_view presents a view of an underlying sequence as constant[.](#overview-1.sentence-1)

That is, the elements of an as_const_view cannot be modified[.](#overview-1.sentence-2)

[2](#overview-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L10032)

The name views​::​as_const denotes
a range adaptor object ([[range.adaptor.object]](range.adaptor.object "25.7.2 Range adaptor objects"))[.](#overview-2.sentence-1)

Let E be an expression,
let T be decltype((E)), and
let U be remove_cvref_t<T>[.](#overview-2.sentence-2)

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

- [(2.1)](#overview-2.1)

  If views​::​all_t<T> models [constant_range](range.refinements#concept:constant_range "25.4.6 Other range refinements [range.refinements]"),
then views​::​all(E)[.](#overview-2.1.sentence-1)

- [(2.2)](#overview-2.2)

  Otherwise,
if U denotes empty_view<X> for some type X, then auto(views​::​empty<const X>)[.](#overview-2.2.sentence-1)

- [(2.3)](#overview-2.3)

  Otherwise,
if U denotes span<X, Extent> for some type X and some extent Extent,
then span<const X, Extent>(E)[.](#overview-2.3.sentence-1)

- [(2.4)](#overview-2.4)

  Otherwise,
if U denotes ref_view<X> for some type X andconst X models [constant_range](range.refinements#concept:constant_range "25.4.6 Other range refinements [range.refinements]"),
then ref_view(static_cast<const X&>(E.base()))[.](#overview-2.4.sentence-1)

- [(2.5)](#overview-2.5)

  Otherwise,
if E is an lvalue,const U models [constant_range](range.refinements#concept:constant_range "25.4.6 Other range refinements [range.refinements]"), andU does not model [view](range.view#concept:view "25.4.5 Views [range.view]"),
then ref_view(static_cast<const U&>(E))[.](#overview-2.5.sentence-1)

- [(2.6)](#overview-2.6)

  Otherwise, as_const_view(E)[.](#overview-2.6.sentence-1)

[3](#overview-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L10067)

[*Example [1](#overview-example-1)*: template<[constant_range](range.refinements#concept:constant_range "25.4.6 Other range refinements [range.refinements]") 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*]

#### [25.7.22.2](#view) Class template as_const_view [[range.as.const.view]](range.as.const.view)

[ð](#lib:as_const_view::iterator)

namespace std::ranges {template<[view](range.view#concept:view "25.4.5 Views [range.view]") V>requires [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]")<V>class as_const_view : public view_interface<as_const_view<V>> { V *base_* = V(); // *exposition only*public: as_const_view() requires [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<V> = default; constexpr explicit as_const_view(V base); constexpr V base() const & requires [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14 Concept copy_­constructible [concept.copyconstructible]")<V> { return *base_*; }constexpr V base() && { return std::move(*base_*); }constexpr auto begin() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>) { return ranges::cbegin(*base_*); }constexpr auto begin() const requires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> { return ranges::cbegin(*base_*); }constexpr auto end() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>) { return ranges::cend(*base_*); }constexpr auto end() const requires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> { return ranges::cend(*base_*); }constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V> { return ranges::size(*base_*); }constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V> { return ranges::size(*base_*); }constexpr auto reserve_hint() requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<V>{ return ranges::reserve_hint(*base_*); }constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<const V>{ return ranges::reserve_hint(*base_*); }}; template<class R> as_const_view(R&&) -> as_const_view<views::all_t<R>>;}

[ð](#lib:as_const_view::iterator,constructor)

`constexpr explicit as_const_view(V base);
`

[1](#view-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L10122)

*Effects*: Initializes *base_* with std​::​move(base)[.](#view-1.sentence-1)
