From Jason Turner

[range.utility.conv]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpb12u7kn7/{from.md → to.md} +174 -0
tmp/tmpb12u7kn7/{from.md → to.md} RENAMED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Range conversions <a id="range.utility.conv">[[range.utility.conv]]</a>
2
+
3
+ #### General <a id="range.utility.conv.general">[[range.utility.conv.general]]</a>
4
+
5
+ The range conversion functions construct an object (usually a container)
6
+ from a range, by using a constructor taking a range, a `from_range_t`
7
+ tagged constructor, or a constructor taking a pair of iterators, or by
8
+ inserting each element of the range into the default-constructed object.
9
+
10
+ `ranges::to` is applied recursively, allowing the conversion of a range
11
+ of ranges.
12
+
13
+ [*Example 1*:
14
+
15
+ ``` cpp
16
+ string_view str = "the quick brown fox";
17
+ auto words = views::split(str, ' ') | to<vector<string>>();
18
+ // words is vector<string>{"the", "quick", "brown", "fox"}
19
+ ```
20
+
21
+ — *end example*]
22
+
23
+ Let *`reservable-container`* be defined as follows:
24
+
25
+ ``` cpp
26
+ template<class Container>
27
+ constexpr bool reservable-container = // exposition only
28
+ sized_range<Container> &&
29
+ requires(Container& c, range_size_t<Container> n) {
30
+ c.reserve(n);
31
+ { c.capacity() } -> same_as<decltype(n)>;
32
+ { c.max_size() } -> same_as<decltype(n)>;
33
+ };
34
+ ```
35
+
36
+ Let *`container-insertable`* be defined as follows:
37
+
38
+ ``` cpp
39
+ template<class Container, class Ref>
40
+ constexpr bool container-insertable = // exposition only
41
+ requires(Container& c, Ref&& ref) {
42
+ requires (requires { c.push_back(std::forward<Ref>(ref)); } ||
43
+ requires { c.insert(c.end(), std::forward<Ref>(ref)); });
44
+ };
45
+ ```
46
+
47
+ Let *`container-inserter`* be defined as follows:
48
+
49
+ ``` cpp
50
+ template<class Ref, class Container>
51
+ constexpr auto container-inserter(Container& c) { // exposition only
52
+ if constexpr (requires { c.push_back(declval<Ref>()); })
53
+ return back_inserter(c);
54
+ else
55
+ return inserter(c, c.end());
56
+ }
57
+ ```
58
+
59
+ #### `ranges::to` <a id="range.utility.conv.to">[[range.utility.conv.to]]</a>
60
+
61
+ ``` cpp
62
+ template<class C, input_range R, class... Args> requires (!view<C>)
63
+ constexpr C to(R&& r, Args&&... args);
64
+ ```
65
+
66
+ *Mandates:* `C` is a cv-unqualified class type.
67
+
68
+ *Returns:* An object of type `C` constructed from the elements of `r` in
69
+ the following manner:
70
+
71
+ - If `C` does not satisfy `input_range` or
72
+ `convertible_to<range_reference_t<R>, range_value_t<C>>` is `true`:
73
+ - If `constructible_from<C, R, Args...>` is `true`:
74
+ ``` cpp
75
+ C(std::forward<R>(r), std::forward<Args>(args)...)
76
+ ```
77
+ - Otherwise, if `constructible_from<C, from_range_t, R, Args...>` is
78
+ `true`:
79
+ ``` cpp
80
+ C(from_range, std::forward<R>(r), std::forward<Args>(args)...)
81
+ ```
82
+ - Otherwise, if
83
+ - `common_range<R>` is `true`,
84
+ - the *qualified-id*
85
+ `iterator_traits<iterator_t<R>>::iterator_category` is valid and
86
+ denotes a type that models `derived_from<input_iterator_tag>`, and
87
+ - `constructible_from<C, iterator_t<R>, sentinel_t<R>, Args...>` is
88
+ `true`:
89
+
90
+ ``` cpp
91
+ C(ranges::begin(r), ranges::end(r), std::forward<Args>(args)...)
92
+ ```
93
+ - Otherwise, if
94
+ - `constructible_from<C, Args...>` is `true`, and
95
+ - *`container-insertable`*`<C, range_reference_t<R>>` is `true`:
96
+
97
+ ``` cpp
98
+ C c(std::forward<Args>(args)...);
99
+ if constexpr (sized_range<R> && reservable-container<C>)
100
+ c.reserve(static_cast<range_size_t<C>>(ranges::size(r)));
101
+ ranges::copy(r, container-inserter<range_reference_t<R>>(c));
102
+ ```
103
+ - Otherwise, if `input_range<range_reference_t<R>>` is `true`:
104
+ ``` cpp
105
+ to<C>(r | views::transform([](auto&& elem) {
106
+ return to<range_value_t<C>>(std::forward<decltype(elem)>(elem));
107
+ }), std::forward<Args>(args)...);
108
+ ```
109
+ - Otherwise, the program is ill-formed.
110
+
111
+ ``` cpp
112
+ template<template<class...> class C, input_range R, class... Args>
113
+ constexpr auto to(R&& r, Args&&... args);
114
+ ```
115
+
116
+ Let *input-iterator* be an exposition-only type:
117
+
118
+ ``` cpp
119
+ struct input-iterator { // exposition only
120
+ using iterator_category = input_iterator_tag;
121
+ using value_type = range_value_t<R>;
122
+ using difference_type = ptrdiff_t;
123
+ using pointer = add_pointer_t<range_reference_t<R>>;
124
+ using reference = range_reference_t<R>;
125
+ reference operator*() const;
126
+ pointer operator->() const;
127
+ input-iterator& operator++();
128
+ input-iterator operator++(int);
129
+ bool operator==(const input-iterator&) const;
130
+ };
131
+ ```
132
+
133
+ [*Note 1*: *input-iterator* meets the syntactic requirements of
134
+ *Cpp17InputIterator*. — *end note*]
135
+
136
+ Let *`DEDUCE_EXPR`* be defined as follows:
137
+
138
+ - `C(declval<R>(), declval<Args>()...)` if that is a valid expression,
139
+ - otherwise, `C(from_range, declval<R>(), declval<Args>()...)` if that
140
+ is a valid expression,
141
+ - otherwise,
142
+ ``` cpp
143
+ C(declval<input-iterator>(), declval<input-iterator>(), declval<Args>()...)
144
+ ```
145
+
146
+ if that is a valid expression,
147
+ - otherwise, the program is ill-formed.
148
+
149
+ *Returns:*
150
+ `to<decltype(`*`DEDUCE_EXPR`*`)>(std::forward<R>(r), std::forward<Args>(args)...)`.
151
+
152
+ #### `ranges::to` adaptors <a id="range.utility.conv.adaptors">[[range.utility.conv.adaptors]]</a>
153
+
154
+ ``` cpp
155
+ template<class C, class... Args> requires (!view<C>)
156
+ constexpr auto to(Args&&... args);
157
+ template<template<class...> class C, class... Args>
158
+ constexpr auto to(Args&&... args);
159
+ ```
160
+
161
+ *Mandates:* For the first overload, `C` is a cv-unqualified class type.
162
+
163
+ *Returns:* A range adaptor closure object [[range.adaptor.object]] `f`
164
+ that is a perfect forwarding call
165
+ wrapper [[term.perfect.forwarding.call.wrapper]] with the following
166
+ properties:
167
+
168
+ - It has no target object.
169
+ - Its bound argument entities `bound_args` consist of objects of types
170
+ `decay_t<Args>...` direct-non-list-initialized with
171
+ `std::forward<Args>(args)...`, respectively.
172
+ - Its call pattern is `to<C>(r, bound_args...)`, where `r` is the
173
+ argument used in a function call expression of `f`.
174
+