2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CXX03___RANGES_TO_H
11 #define _LIBCPP___CXX03___RANGES_TO_H
13 #include <__cxx03/__algorithm/ranges_copy.h>
14 #include <__cxx03/__concepts/constructible.h>
15 #include <__cxx03/__concepts/convertible_to.h>
16 #include <__cxx03/__concepts/derived_from.h>
17 #include <__cxx03/__concepts/same_as.h>
18 #include <__cxx03/__config>
19 #include <__cxx03/__functional/bind_back.h>
20 #include <__cxx03/__iterator/back_insert_iterator.h>
21 #include <__cxx03/__iterator/insert_iterator.h>
22 #include <__cxx03/__iterator/iterator_traits.h>
23 #include <__cxx03/__ranges/access.h>
24 #include <__cxx03/__ranges/concepts.h>
25 #include <__cxx03/__ranges/from_range.h>
26 #include <__cxx03/__ranges/range_adaptor.h>
27 #include <__cxx03/__ranges/ref_view.h>
28 #include <__cxx03/__ranges/size.h>
29 #include <__cxx03/__ranges/transform_view.h>
30 #include <__cxx03/__type_traits/add_pointer.h>
31 #include <__cxx03/__type_traits/is_const.h>
32 #include <__cxx03/__type_traits/is_volatile.h>
33 #include <__cxx03/__type_traits/type_identity.h>
34 #include <__cxx03/__utility/declval.h>
35 #include <__cxx03/__utility/forward.h>
36 #include <__cxx03/cstddef>
38 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
39 # pragma GCC system_header
42 _LIBCPP_BEGIN_NAMESPACE_STD
44 #if _LIBCPP_STD_VER >= 23
48 template <class _Container
>
49 constexpr bool __reservable_container
=
50 sized_range
<_Container
> && requires(_Container
& __c
, range_size_t
<_Container
> __n
) {
52 { __c
.capacity() } -> same_as
<decltype(__n
)>;
53 { __c
.max_size() } -> same_as
<decltype(__n
)>;
56 template <class _Container
, class _Ref
>
57 constexpr bool __container_insertable
= requires(_Container
& __c
, _Ref
&& __ref
) {
59 requires
{ __c
.push_back(std::forward
<_Ref
>(__ref
)); } ||
60 requires
{ __c
.insert(__c
.end(), std::forward
<_Ref
>(__ref
)); });
63 template <class _Ref
, class _Container
>
64 _LIBCPP_HIDE_FROM_ABI
constexpr auto __container_inserter(_Container
& __c
) {
65 if constexpr (requires
{ __c
.push_back(std::declval
<_Ref
>()); }) {
66 return std::back_inserter(__c
);
68 return std::inserter(__c
, __c
.end());
72 // Note: making this a concept allows short-circuiting the second condition.
73 template <class _Container
, class _Range
>
74 concept __try_non_recursive_conversion
=
75 !input_range
<_Container
> || convertible_to
<range_reference_t
<_Range
>, range_value_t
<_Container
>>;
77 template <class _Container
, class _Range
, class... _Args
>
78 concept __constructible_from_iter_pair
=
79 common_range
<_Range
> && requires
{ typename iterator_traits
<iterator_t
<_Range
>>::iterator_category
; } &&
80 derived_from
<typename iterator_traits
<iterator_t
<_Range
>>::iterator_category
, input_iterator_tag
> &&
81 constructible_from
<_Container
, iterator_t
<_Range
>, sentinel_t
<_Range
>, _Args
...>;
84 concept __always_false
= false;
86 // `ranges::to` base template -- the `_Container` type is a simple type template parameter.
87 template <class _Container
, input_range _Range
, class... _Args
>
88 requires(!view
<_Container
>)
89 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Container
to(_Range
&& __range
, _Args
&&... __args
) {
90 // Mandates: C is a cv-unqualified class type.
91 static_assert(!is_const_v
<_Container
>, "The target container cannot be const-qualified, please remove the const");
93 !is_volatile_v
<_Container
>, "The target container cannot be volatile-qualified, please remove the volatile");
95 // First see if the non-recursive case applies -- the conversion target is either:
96 // - a range with a convertible value type;
97 // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
98 if constexpr (__try_non_recursive_conversion
<_Container
, _Range
>) {
99 // Case 1 -- construct directly from the given range.
100 if constexpr (constructible_from
<_Container
, _Range
, _Args
...>) {
101 return _Container(std::forward
<_Range
>(__range
), std::forward
<_Args
>(__args
)...);
104 // Case 2 -- construct using the `from_range_t` tagged constructor.
105 else if constexpr (constructible_from
<_Container
, from_range_t
, _Range
, _Args
...>) {
106 return _Container(from_range
, std::forward
<_Range
>(__range
), std::forward
<_Args
>(__args
)...);
109 // Case 3 -- construct from a begin-end iterator pair.
110 else if constexpr (__constructible_from_iter_pair
<_Container
, _Range
, _Args
...>) {
111 return _Container(ranges::begin(__range
), ranges::end(__range
), std::forward
<_Args
>(__args
)...);
114 // Case 4 -- default-construct (or construct from the extra arguments) and insert, reserving the size if possible.
115 else if constexpr (constructible_from
<_Container
, _Args
...> &&
116 __container_insertable
<_Container
, range_reference_t
<_Range
>>) {
117 _Container
__result(std::forward
<_Args
>(__args
)...);
118 if constexpr (sized_range
<_Range
> && __reservable_container
<_Container
>) {
119 __result
.reserve(static_cast<range_size_t
<_Container
>>(ranges::size(__range
)));
122 ranges::copy(__range
, ranges::__container_inserter
<range_reference_t
<_Range
>>(__result
));
127 static_assert(__always_false
<_Container
>, "ranges::to: unable to convert to the given container type.");
130 // Try the recursive case.
131 } else if constexpr (input_range
<range_reference_t
<_Range
>>) {
132 return ranges::to
<_Container
>(
133 ref_view(__range
) | views::transform([](auto&& __elem
) {
134 return ranges::to
<range_value_t
<_Container
>>(std::forward
<decltype(__elem
)>(__elem
));
136 std::forward
<_Args
>(__args
)...);
139 static_assert(__always_false
<_Container
>, "ranges::to: unable to convert to the given container type.");
143 template <class _Range
>
144 struct __minimal_input_iterator
{
145 using iterator_category
= input_iterator_tag
;
146 using value_type
= range_value_t
<_Range
>;
147 using difference_type
= ptrdiff_t;
148 using pointer
= add_pointer_t
<range_reference_t
<_Range
>>;
149 using reference
= range_reference_t
<_Range
>;
151 reference
operator*() const;
152 pointer
operator->() const;
153 __minimal_input_iterator
& operator++();
154 __minimal_input_iterator
operator++(int);
155 bool operator==(const __minimal_input_iterator
&) const;
158 // Deduces the full type of the container from the given template template parameter.
159 template <template <class...> class _Container
, input_range _Range
, class... _Args
>
161 _LIBCPP_HIDE_FROM_ABI
static constexpr auto __deduce_func() {
162 using _InputIter
= __minimal_input_iterator
<_Range
>;
164 // Case 1 -- can construct directly from the given range.
165 if constexpr (requires
{ _Container(std::declval
<_Range
>(), std::declval
<_Args
>()...); }) {
166 using _Result
= decltype( //
167 _Container(std::declval
<_Range
>(), std::declval
<_Args
>()...));
168 return type_identity
<_Result
>{};
170 // Case 2 -- can construct from the given range using the `from_range_t` tagged constructor.
171 } else if constexpr ( //
172 requires
{ _Container(from_range
, std::declval
<_Range
>(), std::declval
<_Args
>()...); }) {
174 decltype(_Container(from_range
, std::declval
<_Range
>(), std::declval
<_Args
>()...));
175 return type_identity
<_Result
>{};
177 // Case 3 -- can construct from a begin-end iterator pair.
178 } else if constexpr ( //
179 requires
{ _Container(std::declval
<_InputIter
>(), std::declval
<_InputIter
>(), std::declval
<_Args
>()...); }) {
181 decltype(_Container(std::declval
<_InputIter
>(), std::declval
<_InputIter
>(), std::declval
<_Args
>()...));
182 return type_identity
<_Result
>{};
185 static_assert(__always_false
<_Range
>,
186 "ranges::to: unable to deduce the container type from the template template argument.");
190 using type
= typename
decltype(__deduce_func())::type
;
193 // `ranges::to` specialization -- `_Container` is a template template parameter requiring deduction to figure out the
194 // container element type.
195 template <template <class...> class _Container
, input_range _Range
, class... _Args
>
196 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr auto to(_Range
&& __range
, _Args
&&... __args
) {
197 using _DeduceExpr
= typename _Deducer
<_Container
, _Range
, _Args
...>::type
;
198 return ranges::to
<_DeduceExpr
>(std::forward
<_Range
>(__range
), std::forward
<_Args
>(__args
)...);
201 // Range adaptor closure object 1 -- wrapping the `ranges::to` version where `_Container` is a simple type template
203 template <class _Container
, class... _Args
>
204 requires(!view
<_Container
>)
205 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr auto to(_Args
&&... __args
) {
206 // Mandates: C is a cv-unqualified class type.
207 static_assert(!is_const_v
<_Container
>, "The target container cannot be const-qualified, please remove the const");
209 !is_volatile_v
<_Container
>, "The target container cannot be volatile-qualified, please remove the volatile");
211 auto __to_func
= []<input_range _Range
, class... _Tail
>(_Range
&& __range
, _Tail
&&... __tail
) static
212 requires requires
{ //
213 /**/ ranges::to
<_Container
>(std::forward
<_Range
>(__range
), std::forward
<_Tail
>(__tail
)...);
215 { return ranges::to
<_Container
>(std::forward
<_Range
>(__range
), std::forward
<_Tail
>(__tail
)...); };
217 return __range_adaptor_closure_t(std::__bind_back(__to_func
, std::forward
<_Args
>(__args
)...));
220 // Range adaptor closure object 2 -- wrapping the `ranges::to` version where `_Container` is a template template
222 template <template <class...> class _Container
, class... _Args
>
223 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr auto to(_Args
&&... __args
) {
225 auto __to_func
= []<input_range _Range
, class... _Tail
,
226 class _DeducedExpr
= typename _Deducer
<_Container
, _Range
, _Tail
...>::type
>
227 (_Range
&& __range
, _Tail
&& ... __tail
) static
228 requires requires
{ //
229 /**/ ranges::to
<_DeducedExpr
>(std::forward
<_Range
>(__range
), std::forward
<_Tail
>(__tail
)...);
232 return ranges::to
<_DeducedExpr
>(std::forward
<_Range
>(__range
), std::forward
<_Tail
>(__tail
)...);
236 return __range_adaptor_closure_t(std::__bind_back(__to_func
, std::forward
<_Args
>(__args
)...));
239 } // namespace ranges
241 #endif // _LIBCPP_STD_VER >= 23
243 _LIBCPP_END_NAMESPACE_STD
245 #endif // _LIBCPP___CXX03___RANGES_TO_H