blob: 27b81a0305e58641a6da8d4ef9cfc17f1b3a3e72 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- utility -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_UTILITY
12#define _LIBCPP_UTILITY
13
14/*
15 utility synopsis
16
17namespace std
18{
19
20template <class T>
21 void
22 swap(T& a, T& b);
23
24namespace rel_ops
25{
26 template<class T> bool operator!=(const T&, const T&);
27 template<class T> bool operator> (const T&, const T&);
28 template<class T> bool operator<=(const T&, const T&);
29 template<class T> bool operator>=(const T&, const T&);
30}
31
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000032template<class T>
33void
34swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
35 is_nothrow_move_assignable<T>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000037template <class T, size_t N>
38void
39swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
40
Marshall Clow01a0e902013-07-15 20:46:11 +000041template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14
42template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000043
Marshall Clow01a0e902013-07-15 20:46:11 +000044template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
46template <class T>
47 typename conditional
48 <
Howard Hinnant1468b662010-11-19 22:17:28 +000049 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050 const T&,
51 T&&
52 >::type
Marshall Clow01a0e902013-07-15 20:46:11 +000053 move_if_noexcept(T& x) noexcept; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054
Marshall Clowf60d0922015-11-17 00:08:08 +000055template <class T> constexpr add_const<T>_t& as_const(T& t) noexcept; // C++17
56template <class T> void as_const(const T&&) = delete; // C++17
57
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
59
60template <class T1, class T2>
61struct pair
62{
63 typedef T1 first_type;
64 typedef T2 second_type;
65
66 T1 first;
67 T2 second;
68
69 pair(const pair&) = default;
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000070 pair(pair&&) = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071 constexpr pair();
Marshall Clow206f6cd2013-07-16 17:45:44 +000072 pair(const T1& x, const T2& y); // constexpr in C++14
73 template <class U, class V> pair(U&& x, V&& y); // constexpr in C++14
74 template <class U, class V> pair(const pair<U, V>& p); // constexpr in C++14
75 template <class U, class V> pair(pair<U, V>&& p); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076 template <class... Args1, class... Args2>
77 pair(piecewise_construct_t, tuple<Args1...> first_args,
78 tuple<Args2...> second_args);
79
80 template <class U, class V> pair& operator=(const pair<U, V>& p);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000081 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
82 is_nothrow_move_assignable<T2>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 template <class U, class V> pair& operator=(pair<U, V>&& p);
84
Eric Fiselier8f1e73d2016-04-21 23:38:59 +000085 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
86 is_nothrow_swappable_v<T2>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000087};
88
Marshall Clow206f6cd2013-07-16 17:45:44 +000089template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
90template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
91template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
92template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
93template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
94template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095
Marshall Clow206f6cd2013-07-16 17:45:44 +000096template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000097template <class T1, class T2>
98void
99swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
101struct piecewise_construct_t { };
102constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
103
104template <class T> class tuple_size;
105template <size_t I, class T> class tuple_element;
106
Marshall Clow50fe0c72014-03-03 06:18:11 +0000107template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
108template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
109template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110
111template<size_t I, class T1, class T2>
Marshall Clow50fe0c72014-03-03 06:18:11 +0000112 typename tuple_element<I, pair<T1, T2> >::type&
113 get(pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114
115template<size_t I, class T1, class T2>
Marshall Clowa6607572015-11-19 19:45:29 +0000116 const typename tuple_element<I, pair<T1, T2> >::type&
Marshall Clow50fe0c72014-03-03 06:18:11 +0000117 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000119template<size_t I, class T1, class T2>
Marshall Clow50fe0c72014-03-03 06:18:11 +0000120 typename tuple_element<I, pair<T1, T2> >::type&&
121 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000122
Eric Fiselier199bee02015-12-18 00:36:55 +0000123template<size_t I, class T1, class T2>
124 const typename tuple_element<I, pair<T1, T2> >::type&&
125 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
126
Marshall Clowe8029e52013-07-13 02:54:05 +0000127template<class T1, class T2>
Marshall Clow50fe0c72014-03-03 06:18:11 +0000128 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
Marshall Clowe8029e52013-07-13 02:54:05 +0000129
Eric Fiselier199bee02015-12-18 00:36:55 +0000130template<class T1, class T2>
Marshall Clowa6607572015-11-19 19:45:29 +0000131 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
Marshall Clowe8029e52013-07-13 02:54:05 +0000132
Eric Fiselier199bee02015-12-18 00:36:55 +0000133template<class T1, class T2>
Marshall Clow50fe0c72014-03-03 06:18:11 +0000134 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
Marshall Clowe8029e52013-07-13 02:54:05 +0000135
Eric Fiselier199bee02015-12-18 00:36:55 +0000136template<class T1, class T2>
137 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
138
139template<class T1, class T2>
140 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
141
142template<class T1, class T2>
143 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
144
145template<class T1, class T2>
146 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
147
148template<class T1, class T2>
149 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
150
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000151// C++14
152
153template<class T, T... I>
154struct integer_sequence
155{
156 typedef T value_type;
157
158 static constexpr size_t size() noexcept;
159};
160
161template<size_t... I>
162 using index_sequence = integer_sequence<size_t, I...>;
163
164template<class T, T N>
165 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
166template<size_t N>
167 using make_index_sequence = make_integer_sequence<size_t, N>;
168
169template<class... T>
170 using index_sequence_for = make_index_sequence<sizeof...(T)>;
171
Marshall Clowe2735d12013-07-08 20:54:40 +0000172template<class T, class U=T>
173 T exchange(T& obj, U&& new_value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174} // std
175
176*/
177
178#include <__config>
179#include <__tuple>
180#include <type_traits>
Ben Craigd59d4162016-04-19 20:13:55 +0000181#include <initializer_list>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182
Howard Hinnant08e17472011-10-17 20:05:10 +0000183#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000185#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186
187_LIBCPP_BEGIN_NAMESPACE_STD
188
189namespace rel_ops
190{
191
192template<class _Tp>
193inline _LIBCPP_INLINE_VISIBILITY
194bool
195operator!=(const _Tp& __x, const _Tp& __y)
196{
197 return !(__x == __y);
198}
199
200template<class _Tp>
201inline _LIBCPP_INLINE_VISIBILITY
202bool
203operator> (const _Tp& __x, const _Tp& __y)
204{
205 return __y < __x;
206}
207
208template<class _Tp>
209inline _LIBCPP_INLINE_VISIBILITY
210bool
211operator<=(const _Tp& __x, const _Tp& __y)
212{
213 return !(__y < __x);
214}
215
216template<class _Tp>
217inline _LIBCPP_INLINE_VISIBILITY
218bool
219operator>=(const _Tp& __x, const _Tp& __y)
220{
221 return !(__x < __y);
222}
223
224} // rel_ops
225
226// swap_ranges
227
Marshall Clowfd8ed7f2015-01-06 19:20:49 +0000228
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229template <class _ForwardIterator1, class _ForwardIterator2>
230inline _LIBCPP_INLINE_VISIBILITY
231_ForwardIterator2
232swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
233{
Eric Fiselierb9919752014-10-27 19:28:20 +0000234 for(; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 swap(*__first1, *__first2);
236 return __first2;
237}
238
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000239// forward declared in <type_traits>
Howard Hinnant99968442011-11-29 18:15:50 +0000240template<class _Tp, size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000242typename enable_if<
243 __is_swappable<_Tp>::value
244>::type
Howard Hinnant99968442011-11-29 18:15:50 +0000245swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246{
Howard Hinnant99968442011-11-29 18:15:50 +0000247 _VSTD::swap_ranges(__a, __a + _Np, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248}
249
250template <class _Tp>
Marshall Clow01a0e902013-07-15 20:46:11 +0000251inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant73d21a42010-09-04 23:28:19 +0000252#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253typename conditional
254<
Howard Hinnant1468b662010-11-19 22:17:28 +0000255 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256 const _Tp&,
257 _Tp&&
258>::type
Howard Hinnant73d21a42010-09-04 23:28:19 +0000259#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260const _Tp&
261#endif
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000262move_if_noexcept(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000264 return _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000265}
266
Marshall Clowf60d0922015-11-17 00:08:08 +0000267#if _LIBCPP_STD_VER > 14
268template <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
269template <class _Tp> void as_const(const _Tp&&) = delete;
270#endif
271
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000272struct _LIBCPP_TYPE_VIS_ONLY piecewise_construct_t { };
Howard Hinnant616e92d2012-04-03 23:45:46 +0000273#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000275#else
276constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279template <class _T1, class _T2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000280struct _LIBCPP_TYPE_VIS_ONLY pair
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281{
282 typedef _T1 first_type;
283 typedef _T2 second_type;
284
285 _T1 first;
286 _T2 second;
287
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000288 // pair(const pair&) = default;
289 // pair(pair&&) = default;
290
Eric Fiselier9bef1ff2015-12-23 08:20:26 +0000291#ifndef _LIBCPP_HAS_NO_DEFAULT_FUNCTION_TEMPLATE_ARGS
292 template <bool _Dummy = true, class = typename enable_if<
293 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
294 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
295 >::type>
296#endif
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000297 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298
Marshall Clow206f6cd2013-07-16 17:45:44 +0000299 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
300 pair(const _T1& __x, const _T2& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 : first(__x), second(__y) {}
302
Howard Hinnant469d4192011-04-29 18:10:55 +0000303 template<class _U1, class _U2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000304 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant9c59d382011-07-27 19:25:28 +0000305 pair(const pair<_U1, _U2>& __p
306#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantfe592762012-06-09 20:01:23 +0000307 ,typename enable_if<is_convertible<const _U1&, _T1>::value &&
308 is_convertible<const _U2&, _T2>::value>::type* = 0
Howard Hinnant9c59d382011-07-27 19:25:28 +0000309#endif
310 )
Howard Hinnant469d4192011-04-29 18:10:55 +0000311 : first(__p.first), second(__p.second) {}
312
Howard Hinnant65173fe2013-11-14 22:52:25 +0000313#if !defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && _LIBCPP_TRIVIAL_PAIR_COPY_CTOR
Marshall Clow206f6cd2013-07-16 17:45:44 +0000314 _LIBCPP_INLINE_VISIBILITY
315 pair(const pair& __p) = default;
Howard Hinnant65173fe2013-11-14 22:52:25 +0000316#elif !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) || !_LIBCPP_TRIVIAL_PAIR_COPY_CTOR
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000318 pair(const pair& __p)
319 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
320 is_nothrow_copy_constructible<second_type>::value)
321 : first(__p.first),
322 second(__p.second)
323 {
324 }
Marshall Clow206f6cd2013-07-16 17:45:44 +0000325#endif
Howard Hinnant61aa6012011-07-01 19:24:36 +0000326
327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000328 pair& operator=(const pair& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +0000329 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
330 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000331 {
332 first = __p.first;
333 second = __p.second;
334 return *this;
335 }
336
Howard Hinnant73d21a42010-09-04 23:28:19 +0000337#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000338
339 template <class _U1, class _U2,
Howard Hinnantfe592762012-06-09 20:01:23 +0000340 class = typename enable_if<is_convertible<_U1, first_type>::value &&
341 is_convertible<_U2, second_type>::value>::type>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000342 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343 pair(_U1&& __u1, _U2&& __u2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000344 : first(_VSTD::forward<_U1>(__u1)),
345 second(_VSTD::forward<_U2>(__u2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346 {}
347
Howard Hinnant469d4192011-04-29 18:10:55 +0000348 template<class _U1, class _U2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000349 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant469d4192011-04-29 18:10:55 +0000350 pair(pair<_U1, _U2>&& __p,
Howard Hinnantfe592762012-06-09 20:01:23 +0000351 typename enable_if<is_convertible<_U1, _T1>::value &&
352 is_convertible<_U2, _T2>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000353 : first(_VSTD::forward<_U1>(__p.first)),
354 second(_VSTD::forward<_U2>(__p.second)) {}
Howard Hinnant469d4192011-04-29 18:10:55 +0000355
Marshall Clow206f6cd2013-07-16 17:45:44 +0000356#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
357 _LIBCPP_INLINE_VISIBILITY
358 pair(pair&& __p) = default;
359#else
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000361 pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
362 is_nothrow_move_constructible<second_type>::value)
363 : first(_VSTD::forward<first_type>(__p.first)),
364 second(_VSTD::forward<second_type>(__p.second))
365 {
366 }
Marshall Clow206f6cd2013-07-16 17:45:44 +0000367#endif
Howard Hinnant61aa6012011-07-01 19:24:36 +0000368
369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000370 pair&
371 operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
372 is_nothrow_move_assignable<second_type>::value)
373 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000374 first = _VSTD::forward<first_type>(__p.first);
375 second = _VSTD::forward<second_type>(__p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000376 return *this;
377 }
378
Michael J. Spencer626916f2010-12-10 19:47:54 +0000379#ifndef _LIBCPP_HAS_NO_VARIADICS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 template<class _Tuple,
382 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000383 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 pair(_Tuple&& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000385 : first(_VSTD::forward<typename tuple_element<0,
Marshall Clowba6dbf42014-06-24 00:46:19 +0000386 typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<0>(__p))),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000387 second(_VSTD::forward<typename tuple_element<1,
Marshall Clowba6dbf42014-06-24 00:46:19 +0000388 typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<1>(__p)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 {}
390
Michael J. Spencer626916f2010-12-10 19:47:54 +0000391
Howard Hinnant726a76f2010-11-16 21:10:23 +0000392
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000393 template <class... _Args1, class... _Args2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
396 tuple<_Args2...> __second_args)
Howard Hinnant324bb032010-08-22 00:02:43 +0000397 : pair(__pc, __first_args, __second_args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
399 typename __make_tuple_indices<sizeof...(_Args2) >::type())
400 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401
402 template <class _Tuple,
403 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 pair&
406 operator=(_Tuple&& __p)
407 {
408 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
409 typedef typename tuple_element<0, _TupleRef>::type _U0;
410 typedef typename tuple_element<1, _TupleRef>::type _U1;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000411 first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
412 second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413 return *this;
414 }
415
Michael J. Spencer626916f2010-12-10 19:47:54 +0000416#endif // _LIBCPP_HAS_NO_VARIADICS
417
Howard Hinnant73d21a42010-09-04 23:28:19 +0000418#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000419 _LIBCPP_INLINE_VISIBILITY
420 void
Howard Hinnanta5e01212011-05-27 19:08:18 +0000421 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
422 __is_nothrow_swappable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000423 {
Marshall Clow6b0e4192015-09-22 17:50:11 +0000424 using _VSTD::swap;
425 swap(first, __p.first);
426 swap(second, __p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000427 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428private:
Howard Hinnant324bb032010-08-22 00:02:43 +0000429
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430#ifndef _LIBCPP_HAS_NO_VARIADICS
431 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Howard Hinnant5f5859c2011-01-13 20:05:05 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433 pair(piecewise_construct_t,
434 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
435 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Howard Hinnant324bb032010-08-22 00:02:43 +0000436#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437};
438
439template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000440inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441bool
442operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
443{
444 return __x.first == __y.first && __x.second == __y.second;
445}
446
447template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000448inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449bool
450operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
451{
452 return !(__x == __y);
453}
454
455template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000456inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457bool
458operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
459{
460 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
461}
462
463template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000464inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465bool
466operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
467{
468 return __y < __x;
469}
470
471template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000472inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473bool
474operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
475{
476 return !(__x < __y);
477}
478
479template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000480inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481bool
482operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
483{
484 return !(__y < __x);
485}
486
487template <class _T1, class _T2>
488inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaabf2872011-06-01 19:59:32 +0000489typename enable_if
490<
491 __is_swappable<_T1>::value &&
492 __is_swappable<_T2>::value,
493 void
494>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnanta5e01212011-05-27 19:08:18 +0000496 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
497 __is_nothrow_swappable<_T2>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498{
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000499 __x.swap(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500}
501
Howard Hinnant73d21a42010-09-04 23:28:19 +0000502#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504
505template <class _Tp>
Marshall Clowa71f9562014-01-03 22:55:49 +0000506struct __make_pair_return_impl
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507{
508 typedef _Tp type;
509};
510
511template <class _Tp>
Marshall Clowa71f9562014-01-03 22:55:49 +0000512struct __make_pair_return_impl<reference_wrapper<_Tp>>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513{
514 typedef _Tp& type;
515};
516
517template <class _Tp>
518struct __make_pair_return
519{
Marshall Clowa71f9562014-01-03 22:55:49 +0000520 typedef typename __make_pair_return_impl<typename decay<_Tp>::type>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521};
522
523template <class _T1, class _T2>
Marshall Clow206f6cd2013-07-16 17:45:44 +0000524inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
526make_pair(_T1&& __t1, _T2&& __t2)
527{
528 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
Howard Hinnant0949eed2011-06-30 21:18:19 +0000529 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530}
531
Howard Hinnant73d21a42010-09-04 23:28:19 +0000532#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533
534template <class _T1, class _T2>
535inline _LIBCPP_INLINE_VISIBILITY
536pair<_T1,_T2>
537make_pair(_T1 __x, _T2 __y)
538{
539 return pair<_T1, _T2>(__x, __y);
540}
541
Howard Hinnant73d21a42010-09-04 23:28:19 +0000542#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544template <class _T1, class _T2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000545 class _LIBCPP_TYPE_VIS_ONLY tuple_size<pair<_T1, _T2> >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000546 : public integral_constant<size_t, 2> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547
548template <class _T1, class _T2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000549class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550{
551public:
552 typedef _T1 type;
553};
554
555template <class _T1, class _T2>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000556class _LIBCPP_TYPE_VIS_ONLY tuple_element<1, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557{
558public:
559 typedef _T2 type;
560};
561
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562template <size_t _Ip> struct __get_pair;
563
564template <>
565struct __get_pair<0>
566{
567 template <class _T1, class _T2>
568 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000569 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000571 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573 template <class _T1, class _T2>
574 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000575 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 const _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000577 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000578
579#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
580
581 template <class _T1, class _T2>
582 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000584 _T1&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000585 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000586
Eric Fiselier199bee02015-12-18 00:36:55 +0000587 template <class _T1, class _T2>
588 static
589 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
590 const _T1&&
591 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
592
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000593#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594};
595
596template <>
597struct __get_pair<1>
598{
599 template <class _T1, class _T2>
600 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000601 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000603 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604
605 template <class _T1, class _T2>
606 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000607 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608 const _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000609 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000610
611#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
612
613 template <class _T1, class _T2>
614 static
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000616 _T2&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000617 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000618
Eric Fiselier199bee02015-12-18 00:36:55 +0000619 template <class _T1, class _T2>
620 static
621 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
622 const _T2&&
623 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
624
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000625#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626};
627
628template <size_t _Ip, class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000629inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000631get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632{
633 return __get_pair<_Ip>::get(__p);
634}
635
636template <size_t _Ip, class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000637inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000639get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640{
641 return __get_pair<_Ip>::get(__p);
642}
643
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000644#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
645
646template <size_t _Ip, class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000647inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000648typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000649get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000650{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000651 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000652}
653
Eric Fiselier199bee02015-12-18 00:36:55 +0000654template <size_t _Ip, class _T1, class _T2>
655inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
656const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
657get(const pair<_T1, _T2>&& __p) _NOEXCEPT
658{
659 return __get_pair<_Ip>::get(_VSTD::move(__p));
660}
661
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000662#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
663
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000664#if _LIBCPP_STD_VER > 11
Marshall Clowe8029e52013-07-13 02:54:05 +0000665template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000666inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000667constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
668{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000669 return __get_pair<0>::get(__p);
Marshall Clowe8029e52013-07-13 02:54:05 +0000670}
671
672template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000673inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000674constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
675{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000676 return __get_pair<0>::get(__p);
Marshall Clowe8029e52013-07-13 02:54:05 +0000677}
678
679template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000680inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000681constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
682{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000683 return __get_pair<0>::get(_VSTD::move(__p));
Marshall Clowe8029e52013-07-13 02:54:05 +0000684}
685
686template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000687inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier199bee02015-12-18 00:36:55 +0000688constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
689{
690 return __get_pair<0>::get(_VSTD::move(__p));
691}
692
693template <class _T1, class _T2>
694inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000695constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
696{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000697 return __get_pair<1>::get(__p);
Marshall Clowe8029e52013-07-13 02:54:05 +0000698}
699
700template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000701inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000702constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
703{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000704 return __get_pair<1>::get(__p);
Marshall Clowe8029e52013-07-13 02:54:05 +0000705}
706
707template <class _T1, class _T2>
Howard Hinnant1e564242013-10-04 22:09:00 +0000708inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe8029e52013-07-13 02:54:05 +0000709constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
710{
Marshall Clow206f6cd2013-07-16 17:45:44 +0000711 return __get_pair<1>::get(_VSTD::move(__p));
Marshall Clowe8029e52013-07-13 02:54:05 +0000712}
713
Eric Fiselier199bee02015-12-18 00:36:55 +0000714template <class _T1, class _T2>
715inline _LIBCPP_INLINE_VISIBILITY
716constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
717{
718 return __get_pair<1>::get(_VSTD::move(__p));
719}
720
Marshall Clowe8029e52013-07-13 02:54:05 +0000721#endif
722
723#if _LIBCPP_STD_VER > 11
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000724
725template<class _Tp, _Tp... _Ip>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000726struct _LIBCPP_TYPE_VIS_ONLY integer_sequence
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000727{
728 typedef _Tp value_type;
729 static_assert( is_integral<_Tp>::value,
730 "std::integer_sequence can only be instantiated with an integral type" );
731 static
732 _LIBCPP_INLINE_VISIBILITY
733 constexpr
734 size_t
735 size() noexcept { return sizeof...(_Ip); }
736};
737
738template<size_t... _Ip>
739 using index_sequence = integer_sequence<size_t, _Ip...>;
740
Eric Fiselier76d24462015-12-09 22:03:06 +0000741#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
742
743template <class _Tp, _Tp _Ep>
744struct __make_integer_sequence
745{
746 typedef __make_integer_seq<integer_sequence, _Tp, _Ep> type;
747};
748
749#else
750
Marshall Clow42e55e92013-07-03 19:20:30 +0000751namespace __detail {
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000752
Marshall Clow42e55e92013-07-03 19:20:30 +0000753template<typename _Tp, size_t ..._Extra> struct __repeat;
754template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<integer_sequence<_Tp, _Np...>, _Extra...> {
755 typedef integer_sequence<_Tp,
756 _Np...,
757 sizeof...(_Np) + _Np...,
758 2 * sizeof...(_Np) + _Np...,
759 3 * sizeof...(_Np) + _Np...,
760 4 * sizeof...(_Np) + _Np...,
761 5 * sizeof...(_Np) + _Np...,
762 6 * sizeof...(_Np) + _Np...,
763 7 * sizeof...(_Np) + _Np...,
764 _Extra...> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000765};
766
Marshall Clow42e55e92013-07-03 19:20:30 +0000767template<size_t _Np> struct __parity;
768template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
769
770template<> struct __make<0> { typedef integer_sequence<size_t> type; };
771template<> struct __make<1> { typedef integer_sequence<size_t, 0> type; };
772template<> struct __make<2> { typedef integer_sequence<size_t, 0, 1> type; };
773template<> struct __make<3> { typedef integer_sequence<size_t, 0, 1, 2> type; };
774template<> struct __make<4> { typedef integer_sequence<size_t, 0, 1, 2, 3> type; };
775template<> struct __make<5> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
776template<> struct __make<6> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
777template<> struct __make<7> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
778
779template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
780template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
781template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
782template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
783template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
784template<> struct __parity<5> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
785template<> struct __parity<6> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
786template<> struct __parity<7> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 7, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
787
788template<typename _Tp, typename _Up> struct __convert {
789 template<typename> struct __result;
790 template<_Tp ..._Np> struct __result<integer_sequence<_Tp, _Np...> > { typedef integer_sequence<_Up, _Np...> type; };
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000791};
Marshall Clow42e55e92013-07-03 19:20:30 +0000792template<typename _Tp> struct __convert<_Tp, _Tp> { template<typename _Up> struct __result { typedef _Up type; }; };
793
794}
795
796template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
797 typename __detail::__convert<size_t, _Tp>::template __result<typename __detail::__make<_Np>::type>::type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000798
799template <class _Tp, _Tp _Ep>
800struct __make_integer_sequence
801{
802 static_assert(is_integral<_Tp>::value,
803 "std::make_integer_sequence can only be instantiated with an integral type" );
Eric Fiselier76d24462015-12-09 22:03:06 +0000804 static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
Eric Fiselierd6a12b32015-12-16 00:35:45 +0000805 // Workaround GCC bug by preventing bad installations when 0 <= _Ep
806 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
807 typedef __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000808};
809
Eric Fiselier76d24462015-12-09 22:03:06 +0000810#endif
811
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000812template<class _Tp, _Tp _Np>
813 using make_integer_sequence = typename __make_integer_sequence<_Tp, _Np>::type;
814
815template<size_t _Np>
816 using make_index_sequence = make_integer_sequence<size_t, _Np>;
817
818template<class... _Tp>
819 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
820
821#endif // _LIBCPP_STD_VER > 11
822
Marshall Clowe2735d12013-07-08 20:54:40 +0000823#if _LIBCPP_STD_VER > 11
824template<class _T1, class _T2 = _T1>
Howard Hinnant1e564242013-10-04 22:09:00 +0000825inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe2735d12013-07-08 20:54:40 +0000826_T1 exchange(_T1& __obj, _T2 && __new_value)
827{
Howard Hinnant171771a2013-07-08 21:06:38 +0000828 _T1 __old_value = _VSTD::move(__obj);
829 __obj = _VSTD::forward<_T2>(__new_value);
830 return __old_value;
831}
Marshall Clowe2735d12013-07-08 20:54:40 +0000832#endif // _LIBCPP_STD_VER > 11
833
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834_LIBCPP_END_NAMESPACE_STD
835
836#endif // _LIBCPP_UTILITY