From Jason Turner

[range.as.const]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpz59f4zht/{from.md → to.md} +74 -0
tmp/tmpz59f4zht/{from.md → to.md} RENAMED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### As const view <a id="range.as.const">[[range.as.const]]</a>
2
+
3
+ #### Overview <a id="range.as.const.overview">[[range.as.const.overview]]</a>
4
+
5
+ `as_const_view` presents a view of an underlying sequence as constant.
6
+ That is, the elements of an `as_const_view` cannot be modified.
7
+
8
+ The name `views::as_const` denotes a range adaptor object
9
+ [[range.adaptor.object]]. Let `E` be an expression, let `T` be
10
+ `decltype((E))`, and let `U` be `remove_cvref_t<T>`. The expression
11
+ `views::as_const(E)` is expression-equivalent to:
12
+
13
+ - If `views::all_t<T>` models `constant_range`, then `views::all(E)`.
14
+ - Otherwise, if `U` denotes `empty_view<X>` for some type `X`, then
15
+ `auto(views::empty<const X>)`.
16
+ - Otherwise, if `U` denotes `span<X, Extent>` for some type `X` and some
17
+ extent `Extent`, then `span<const X, Extent>(E)`.
18
+ - Otherwise, if `U` denotes `ref_view<X>` for some type `X` and
19
+ `const X` models `constant_range`, then
20
+ `ref_view(static_cast<const X&>(E.base()))`.
21
+ - Otherwise, if `E` is an lvalue, `const U` models `constant_range`, and
22
+ `U` does not model `view`, then `ref_view(static_cast<const U&>(E))`.
23
+ - Otherwise, `as_const_view(E)`.
24
+
25
+ [*Example 1*:
26
+
27
+ ``` cpp
28
+ template<constant_range R>
29
+ void cant_touch_this(R&&);
30
+
31
+ vector<char> hammer = {'m', 'c'};
32
+ span<char> beat = hammer;
33
+ cant_touch_this(views::as_const(beat)); // will not modify the elements of hammer
34
+ ```
35
+
36
+ — *end example*]
37
+
38
+ #### Class template `as_const_view` <a id="range.as.const.view">[[range.as.const.view]]</a>
39
+
40
+ ``` cpp
41
+ namespace std::ranges {
42
+ template<view V>
43
+ requires input_range<V>
44
+ class as_const_view : public view_interface<as_const_view<V>> {
45
+ V base_ = V(); // exposition only
46
+
47
+ public:
48
+ as_const_view() requires default_initializable<V> = default;
49
+ constexpr explicit as_const_view(V base);
50
+
51
+ constexpr V base() const & requires copy_constructible<V> { return base_; }
52
+ constexpr V base() && { return std::move(base_); }
53
+
54
+ constexpr auto begin() requires (!simple-view<V>) { return ranges::cbegin(base_); }
55
+ constexpr auto begin() const requires range<const V> { return ranges::cbegin(base_); }
56
+
57
+ constexpr auto end() requires (!simple-view<V>) { return ranges::cend(base_); }
58
+ constexpr auto end() const requires range<const V> { return ranges::cend(base_); }
59
+
60
+ constexpr auto size() requires sized_range<V> { return ranges::size(base_); }
61
+ constexpr auto size() const requires sized_range<const V> { return ranges::size(base_); }
62
+ };
63
+
64
+ template<class R>
65
+ as_const_view(R&&) -> as_const_view<views::all_t<R>>;
66
+ }
67
+ ```
68
+
69
+ ``` cpp
70
+ constexpr explicit as_const_view(V base);
71
+ ```
72
+
73
+ *Effects:* Initializes *base\_* with `std::move(base)`.
74
+