tuple 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. // -*- C++ -*-
  2. //===--------------------------- tuple ------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is distributed under the University of Illinois Open Source
  7. // License. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_TUPLE
  11. #define _LIBCPP_TUPLE
  12. /*
  13. tuple synopsis
  14. namespace std
  15. {
  16. template <class... T>
  17. class tuple {
  18. public:
  19. constexpr tuple();
  20. explicit tuple(const T&...); // constexpr in C++14
  21. template <class... U>
  22. explicit tuple(U&&...); // constexpr in C++14
  23. tuple(const tuple&) = default;
  24. tuple(tuple&&) = default;
  25. template <class... U>
  26. tuple(const tuple<U...>&); // constexpr in C++14
  27. template <class... U>
  28. tuple(tuple<U...>&&); // constexpr in C++14
  29. template <class U1, class U2>
  30. tuple(const pair<U1, U2>&); // iff sizeof...(T) == 2 // constexpr in C++14
  31. template <class U1, class U2>
  32. tuple(pair<U1, U2>&&); // iff sizeof...(T) == 2 // constexpr in C++14
  33. // allocator-extended constructors
  34. template <class Alloc>
  35. tuple(allocator_arg_t, const Alloc& a);
  36. template <class Alloc>
  37. tuple(allocator_arg_t, const Alloc& a, const T&...);
  38. template <class Alloc, class... U>
  39. tuple(allocator_arg_t, const Alloc& a, U&&...);
  40. template <class Alloc>
  41. tuple(allocator_arg_t, const Alloc& a, const tuple&);
  42. template <class Alloc>
  43. tuple(allocator_arg_t, const Alloc& a, tuple&&);
  44. template <class Alloc, class... U>
  45. tuple(allocator_arg_t, const Alloc& a, const tuple<U...>&);
  46. template <class Alloc, class... U>
  47. tuple(allocator_arg_t, const Alloc& a, tuple<U...>&&);
  48. template <class Alloc, class U1, class U2>
  49. tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&);
  50. template <class Alloc, class U1, class U2>
  51. tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&);
  52. tuple& operator=(const tuple&);
  53. tuple&
  54. operator=(tuple&&) noexcept(AND(is_nothrow_move_assignable<T>::value ...));
  55. template <class... U>
  56. tuple& operator=(const tuple<U...>&);
  57. template <class... U>
  58. tuple& operator=(tuple<U...>&&);
  59. template <class U1, class U2>
  60. tuple& operator=(const pair<U1, U2>&); // iff sizeof...(T) == 2
  61. template <class U1, class U2>
  62. tuple& operator=(pair<U1, U2>&&); //iffsizeof...(T) == 2
  63. void swap(tuple&) noexcept(AND(swap(declval<T&>(), declval<T&>())...));
  64. };
  65. const unspecified ignore;
  66. template <class... T> tuple<V...> make_tuple(T&&...); // constexpr in C++14
  67. template <class... T> tuple<ATypes...> forward_as_tuple(T&&...) noexcept; // constexpr in C++14
  68. template <class... T> tuple<T&...> tie(T&...) noexcept; // constexpr in C++14
  69. template <class... Tuples> tuple<CTypes...> tuple_cat(Tuples&&... tpls); // constexpr in C++14
  70. // [tuple.apply], calling a function with a tuple of arguments:
  71. template <class F, class Tuple>
  72. constexpr decltype(auto) apply(F&& f, Tuple&& t); // C++17
  73. template <class T, class Tuple>
  74. constexpr T make_from_tuple(Tuple&& t); // C++17
  75. // 20.4.1.4, tuple helper classes:
  76. template <class T> class tuple_size; // undefined
  77. template <class... T> class tuple_size<tuple<T...>>;
  78. template <class T>
  79. constexpr size_t tuple_size_v = tuple_size<T>::value; // C++17
  80. template <size_t I, class T> class tuple_element; // undefined
  81. template <size_t I, class... T> class tuple_element<I, tuple<T...>>;
  82. template <size_t I, class T>
  83. using tuple_element_t = typename tuple_element <I, T>::type; // C++14
  84. // 20.4.1.5, element access:
  85. template <size_t I, class... T>
  86. typename tuple_element<I, tuple<T...>>::type&
  87. get(tuple<T...>&) noexcept; // constexpr in C++14
  88. template <size_t I, class... T>
  89. const typename tuple_element<I, tuple<T...>>::type&
  90. get(const tuple<T...>&) noexcept; // constexpr in C++14
  91. template <size_t I, class... T>
  92. typename tuple_element<I, tuple<T...>>::type&&
  93. get(tuple<T...>&&) noexcept; // constexpr in C++14
  94. template <size_t I, class... T>
  95. const typename tuple_element<I, tuple<T...>>::type&&
  96. get(const tuple<T...>&&) noexcept; // constexpr in C++14
  97. template <class T1, class... T>
  98. constexpr T1& get(tuple<T...>&) noexcept; // C++14
  99. template <class T1, class... T>
  100. constexpr const T1& get(const tuple<T...>&) noexcept; // C++14
  101. template <class T1, class... T>
  102. constexpr T1&& get(tuple<T...>&&) noexcept; // C++14
  103. template <class T1, class... T>
  104. constexpr const T1&& get(const tuple<T...>&&) noexcept; // C++14
  105. // 20.4.1.6, relational operators:
  106. template<class... T, class... U> bool operator==(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  107. template<class... T, class... U> bool operator<(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  108. template<class... T, class... U> bool operator!=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  109. template<class... T, class... U> bool operator>(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  110. template<class... T, class... U> bool operator<=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  111. template<class... T, class... U> bool operator>=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
  112. template <class... Types, class Alloc>
  113. struct uses_allocator<tuple<Types...>, Alloc>;
  114. template <class... Types>
  115. void
  116. swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(noexcept(x.swap(y)));
  117. } // std
  118. */
  119. #include <__config>
  120. #include <__tuple>
  121. #include <cstddef>
  122. #include <type_traits>
  123. #include <__functional_base>
  124. #include <utility>
  125. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  126. #pragma GCC system_header
  127. #endif
  128. _LIBCPP_BEGIN_NAMESPACE_STD
  129. #ifndef _LIBCPP_HAS_NO_VARIADICS
  130. // tuple_size
  131. template <class ..._Tp>
  132. class _LIBCPP_TYPE_VIS_ONLY tuple_size<tuple<_Tp...> >
  133. : public integral_constant<size_t, sizeof...(_Tp)>
  134. {
  135. };
  136. // tuple_element
  137. template <size_t _Ip, class ..._Tp>
  138. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, tuple<_Tp...> >
  139. {
  140. public:
  141. typedef typename tuple_element<_Ip, __tuple_types<_Tp...> >::type type;
  142. };
  143. #if _LIBCPP_STD_VER > 11
  144. template <size_t _Ip, class ..._Tp>
  145. using tuple_element_t = typename tuple_element <_Ip, _Tp...>::type;
  146. #endif
  147. // __tuple_leaf
  148. template <size_t _Ip, class _Hp,
  149. bool=is_empty<_Hp>::value && !__libcpp_is_final<_Hp>::value
  150. >
  151. class __tuple_leaf;
  152. template <size_t _Ip, class _Hp, bool _Ep>
  153. inline _LIBCPP_INLINE_VISIBILITY
  154. void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
  155. _NOEXCEPT_(__is_nothrow_swappable<_Hp>::value)
  156. {
  157. swap(__x.get(), __y.get());
  158. }
  159. template <size_t _Ip, class _Hp, bool>
  160. class __tuple_leaf
  161. {
  162. _Hp value;
  163. __tuple_leaf& operator=(const __tuple_leaf&);
  164. public:
  165. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __tuple_leaf()
  166. _NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) : value()
  167. {static_assert(!is_reference<_Hp>::value,
  168. "Attempted to default construct a reference element in a tuple");}
  169. template <class _Alloc>
  170. _LIBCPP_INLINE_VISIBILITY
  171. __tuple_leaf(integral_constant<int, 0>, const _Alloc&)
  172. : value()
  173. {static_assert(!is_reference<_Hp>::value,
  174. "Attempted to default construct a reference element in a tuple");}
  175. template <class _Alloc>
  176. _LIBCPP_INLINE_VISIBILITY
  177. __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
  178. : value(allocator_arg_t(), __a)
  179. {static_assert(!is_reference<_Hp>::value,
  180. "Attempted to default construct a reference element in a tuple");}
  181. template <class _Alloc>
  182. _LIBCPP_INLINE_VISIBILITY
  183. __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
  184. : value(__a)
  185. {static_assert(!is_reference<_Hp>::value,
  186. "Attempted to default construct a reference element in a tuple");}
  187. template <class _Tp,
  188. class = typename enable_if<
  189. __lazy_and<
  190. __lazy_not<is_same<typename decay<_Tp>::type, __tuple_leaf>>
  191. , is_constructible<_Hp, _Tp>
  192. >::value
  193. >::type
  194. >
  195. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  196. explicit __tuple_leaf(_Tp&& __t) _NOEXCEPT_((is_nothrow_constructible<_Hp, _Tp>::value))
  197. : value(_VSTD::forward<_Tp>(__t))
  198. {static_assert(!is_reference<_Hp>::value ||
  199. (is_lvalue_reference<_Hp>::value &&
  200. (is_lvalue_reference<_Tp>::value ||
  201. is_same<typename remove_reference<_Tp>::type,
  202. reference_wrapper<
  203. typename remove_reference<_Hp>::type
  204. >
  205. >::value)) ||
  206. (is_rvalue_reference<_Hp>::value &&
  207. !is_lvalue_reference<_Tp>::value),
  208. "Attempted to construct a reference element in a tuple with an rvalue");}
  209. template <class _Tp, class _Alloc>
  210. _LIBCPP_INLINE_VISIBILITY
  211. explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
  212. : value(_VSTD::forward<_Tp>(__t))
  213. {static_assert(!is_lvalue_reference<_Hp>::value ||
  214. (is_lvalue_reference<_Hp>::value &&
  215. (is_lvalue_reference<_Tp>::value ||
  216. is_same<typename remove_reference<_Tp>::type,
  217. reference_wrapper<
  218. typename remove_reference<_Hp>::type
  219. >
  220. >::value)),
  221. "Attempted to construct a reference element in a tuple with an rvalue");}
  222. template <class _Tp, class _Alloc>
  223. _LIBCPP_INLINE_VISIBILITY
  224. explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
  225. : value(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t))
  226. {static_assert(!is_lvalue_reference<_Hp>::value ||
  227. (is_lvalue_reference<_Hp>::value &&
  228. (is_lvalue_reference<_Tp>::value ||
  229. is_same<typename remove_reference<_Tp>::type,
  230. reference_wrapper<
  231. typename remove_reference<_Hp>::type
  232. >
  233. >::value)),
  234. "Attempted to construct a reference element in a tuple with an rvalue");}
  235. template <class _Tp, class _Alloc>
  236. _LIBCPP_INLINE_VISIBILITY
  237. explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
  238. : value(_VSTD::forward<_Tp>(__t), __a)
  239. {static_assert(!is_lvalue_reference<_Hp>::value ||
  240. (is_lvalue_reference<_Hp>::value &&
  241. (is_lvalue_reference<_Tp>::value ||
  242. is_same<typename remove_reference<_Tp>::type,
  243. reference_wrapper<
  244. typename remove_reference<_Hp>::type
  245. >
  246. >::value)),
  247. "Attempted to construct a reference element in a tuple with an rvalue");}
  248. __tuple_leaf(const __tuple_leaf& __t) = default;
  249. __tuple_leaf(__tuple_leaf&& __t) = default;
  250. template <class _Tp>
  251. _LIBCPP_INLINE_VISIBILITY
  252. __tuple_leaf&
  253. operator=(_Tp&& __t) _NOEXCEPT_((is_nothrow_assignable<_Hp&, _Tp>::value))
  254. {
  255. value = _VSTD::forward<_Tp>(__t);
  256. return *this;
  257. }
  258. _LIBCPP_INLINE_VISIBILITY
  259. int swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
  260. {
  261. _VSTD::swap(*this, __t);
  262. return 0;
  263. }
  264. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 _Hp& get() _NOEXCEPT {return value;}
  265. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const _Hp& get() const _NOEXCEPT {return value;}
  266. };
  267. template <size_t _Ip, class _Hp>
  268. class __tuple_leaf<_Ip, _Hp, true>
  269. : private _Hp
  270. {
  271. __tuple_leaf& operator=(const __tuple_leaf&);
  272. public:
  273. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __tuple_leaf()
  274. _NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) {}
  275. template <class _Alloc>
  276. _LIBCPP_INLINE_VISIBILITY
  277. __tuple_leaf(integral_constant<int, 0>, const _Alloc&) {}
  278. template <class _Alloc>
  279. _LIBCPP_INLINE_VISIBILITY
  280. __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
  281. : _Hp(allocator_arg_t(), __a) {}
  282. template <class _Alloc>
  283. _LIBCPP_INLINE_VISIBILITY
  284. __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
  285. : _Hp(__a) {}
  286. template <class _Tp,
  287. class = typename enable_if<
  288. __lazy_and<
  289. __lazy_not<is_same<typename decay<_Tp>::type, __tuple_leaf>>
  290. , is_constructible<_Hp, _Tp>
  291. >::value
  292. >::type
  293. >
  294. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  295. explicit __tuple_leaf(_Tp&& __t) _NOEXCEPT_((is_nothrow_constructible<_Hp, _Tp>::value))
  296. : _Hp(_VSTD::forward<_Tp>(__t)) {}
  297. template <class _Tp, class _Alloc>
  298. _LIBCPP_INLINE_VISIBILITY
  299. explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
  300. : _Hp(_VSTD::forward<_Tp>(__t)) {}
  301. template <class _Tp, class _Alloc>
  302. _LIBCPP_INLINE_VISIBILITY
  303. explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
  304. : _Hp(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t)) {}
  305. template <class _Tp, class _Alloc>
  306. _LIBCPP_INLINE_VISIBILITY
  307. explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
  308. : _Hp(_VSTD::forward<_Tp>(__t), __a) {}
  309. __tuple_leaf(__tuple_leaf const &) = default;
  310. __tuple_leaf(__tuple_leaf &&) = default;
  311. template <class _Tp>
  312. _LIBCPP_INLINE_VISIBILITY
  313. __tuple_leaf&
  314. operator=(_Tp&& __t) _NOEXCEPT_((is_nothrow_assignable<_Hp&, _Tp>::value))
  315. {
  316. _Hp::operator=(_VSTD::forward<_Tp>(__t));
  317. return *this;
  318. }
  319. _LIBCPP_INLINE_VISIBILITY
  320. int
  321. swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
  322. {
  323. _VSTD::swap(*this, __t);
  324. return 0;
  325. }
  326. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 _Hp& get() _NOEXCEPT {return static_cast<_Hp&>(*this);}
  327. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const _Hp& get() const _NOEXCEPT {return static_cast<const _Hp&>(*this);}
  328. };
  329. template <class ..._Tp>
  330. _LIBCPP_INLINE_VISIBILITY
  331. void __swallow(_Tp&&...) _NOEXCEPT {}
  332. template <class ..._Tp>
  333. struct __lazy_all : __all<_Tp::value...> {};
  334. template <class _Tp>
  335. struct __all_default_constructible;
  336. template <class ..._Tp>
  337. struct __all_default_constructible<__tuple_types<_Tp...>>
  338. : __all<is_default_constructible<_Tp>::value...>
  339. { };
  340. // __tuple_impl
  341. template<class _Indx, class ..._Tp> struct __tuple_impl;
  342. template<size_t ..._Indx, class ..._Tp>
  343. struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
  344. : public __tuple_leaf<_Indx, _Tp>...
  345. {
  346. _LIBCPP_INLINE_VISIBILITY
  347. _LIBCPP_CONSTEXPR __tuple_impl()
  348. _NOEXCEPT_(__all<is_nothrow_default_constructible<_Tp>::value...>::value) {}
  349. template <size_t ..._Uf, class ..._Tf,
  350. size_t ..._Ul, class ..._Tl, class ..._Up>
  351. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  352. explicit
  353. __tuple_impl(__tuple_indices<_Uf...>, __tuple_types<_Tf...>,
  354. __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
  355. _Up&&... __u)
  356. _NOEXCEPT_((__all<is_nothrow_constructible<_Tf, _Up>::value...>::value &&
  357. __all<is_nothrow_default_constructible<_Tl>::value...>::value)) :
  358. __tuple_leaf<_Uf, _Tf>(_VSTD::forward<_Up>(__u))...,
  359. __tuple_leaf<_Ul, _Tl>()...
  360. {}
  361. template <class _Alloc, size_t ..._Uf, class ..._Tf,
  362. size_t ..._Ul, class ..._Tl, class ..._Up>
  363. _LIBCPP_INLINE_VISIBILITY
  364. explicit
  365. __tuple_impl(allocator_arg_t, const _Alloc& __a,
  366. __tuple_indices<_Uf...>, __tuple_types<_Tf...>,
  367. __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
  368. _Up&&... __u) :
  369. __tuple_leaf<_Uf, _Tf>(__uses_alloc_ctor<_Tf, _Alloc, _Up>(), __a,
  370. _VSTD::forward<_Up>(__u))...,
  371. __tuple_leaf<_Ul, _Tl>(__uses_alloc_ctor<_Tl, _Alloc>(), __a)...
  372. {}
  373. template <class _Tuple,
  374. class = typename enable_if
  375. <
  376. __tuple_constructible<_Tuple, tuple<_Tp...> >::value
  377. >::type
  378. >
  379. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  380. __tuple_impl(_Tuple&& __t) _NOEXCEPT_((__all<is_nothrow_constructible<_Tp, typename tuple_element<_Indx,
  381. typename __make_tuple_types<_Tuple>::type>::type>::value...>::value))
  382. : __tuple_leaf<_Indx, _Tp>(_VSTD::forward<typename tuple_element<_Indx,
  383. typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
  384. {}
  385. template <class _Alloc, class _Tuple,
  386. class = typename enable_if
  387. <
  388. __tuple_constructible<_Tuple, tuple<_Tp...> >::value
  389. >::type
  390. >
  391. _LIBCPP_INLINE_VISIBILITY
  392. __tuple_impl(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
  393. : __tuple_leaf<_Indx, _Tp>(__uses_alloc_ctor<_Tp, _Alloc, typename tuple_element<_Indx,
  394. typename __make_tuple_types<_Tuple>::type>::type>(), __a,
  395. _VSTD::forward<typename tuple_element<_Indx,
  396. typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
  397. {}
  398. template <class _Tuple>
  399. _LIBCPP_INLINE_VISIBILITY
  400. typename enable_if
  401. <
  402. __tuple_assignable<_Tuple, tuple<_Tp...> >::value,
  403. __tuple_impl&
  404. >::type
  405. operator=(_Tuple&& __t) _NOEXCEPT_((__all<is_nothrow_assignable<_Tp&, typename tuple_element<_Indx,
  406. typename __make_tuple_types<_Tuple>::type>::type>::value...>::value))
  407. {
  408. __swallow(__tuple_leaf<_Indx, _Tp>::operator=(_VSTD::forward<typename tuple_element<_Indx,
  409. typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...);
  410. return *this;
  411. }
  412. __tuple_impl(const __tuple_impl&) = default;
  413. __tuple_impl(__tuple_impl&&) = default;
  414. _LIBCPP_INLINE_VISIBILITY
  415. __tuple_impl&
  416. operator=(const __tuple_impl& __t) _NOEXCEPT_((__all<is_nothrow_copy_assignable<_Tp>::value...>::value))
  417. {
  418. __swallow(__tuple_leaf<_Indx, _Tp>::operator=(static_cast<const __tuple_leaf<_Indx, _Tp>&>(__t).get())...);
  419. return *this;
  420. }
  421. _LIBCPP_INLINE_VISIBILITY
  422. __tuple_impl&
  423. operator=(__tuple_impl&& __t) _NOEXCEPT_((__all<is_nothrow_move_assignable<_Tp>::value...>::value))
  424. {
  425. __swallow(__tuple_leaf<_Indx, _Tp>::operator=(_VSTD::forward<_Tp>(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t).get()))...);
  426. return *this;
  427. }
  428. _LIBCPP_INLINE_VISIBILITY
  429. void swap(__tuple_impl& __t)
  430. _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
  431. {
  432. __swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t))...);
  433. }
  434. };
  435. template <bool _IsTuple, class _SizeTrait, size_t _Expected>
  436. struct __tuple_like_with_size_imp : false_type {};
  437. template <class _SizeTrait, size_t _Expected>
  438. struct __tuple_like_with_size_imp<true, _SizeTrait, _Expected>
  439. : integral_constant<bool, _SizeTrait::value == _Expected> {};
  440. template <class _Tuple, size_t _ExpectedSize,
  441. class _RawTuple = typename __uncvref<_Tuple>::type>
  442. using __tuple_like_with_size = __tuple_like_with_size_imp<
  443. __tuple_like<_RawTuple>::value,
  444. tuple_size<_RawTuple>, _ExpectedSize
  445. >;
  446. struct _LIBCPP_TYPE_VIS __check_tuple_constructor_fail {
  447. template <class ...>
  448. static constexpr bool __enable_explicit() { return false; }
  449. template <class ...>
  450. static constexpr bool __enable_implicit() { return false; }
  451. };
  452. template <class ..._Tp>
  453. class _LIBCPP_TYPE_VIS_ONLY tuple
  454. {
  455. typedef __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, _Tp...> base;
  456. base base_;
  457. template <class ..._Args>
  458. struct _PackExpandsToThisTuple : false_type {};
  459. template <class _Arg>
  460. struct _PackExpandsToThisTuple<_Arg>
  461. : is_same<typename __uncvref<_Arg>::type, tuple> {};
  462. template <bool _MaybeEnable, class _Dummy = void>
  463. struct _CheckArgsConstructor : __check_tuple_constructor_fail {};
  464. template <class _Dummy>
  465. struct _CheckArgsConstructor<true, _Dummy>
  466. {
  467. template <class ..._Args>
  468. static constexpr bool __enable_explicit() {
  469. return
  470. __tuple_constructible<
  471. tuple<_Args...>,
  472. typename __make_tuple_types<tuple,
  473. sizeof...(_Args) < sizeof...(_Tp) ?
  474. sizeof...(_Args) :
  475. sizeof...(_Tp)>::type
  476. >::value &&
  477. !__tuple_convertible<
  478. tuple<_Args...>,
  479. typename __make_tuple_types<tuple,
  480. sizeof...(_Args) < sizeof...(_Tp) ?
  481. sizeof...(_Args) :
  482. sizeof...(_Tp)>::type
  483. >::value &&
  484. __all_default_constructible<
  485. typename __make_tuple_types<tuple, sizeof...(_Tp),
  486. sizeof...(_Args) < sizeof...(_Tp) ?
  487. sizeof...(_Args) :
  488. sizeof...(_Tp)>::type
  489. >::value;
  490. }
  491. template <class ..._Args>
  492. static constexpr bool __enable_implicit() {
  493. return
  494. __tuple_convertible<
  495. tuple<_Args...>,
  496. typename __make_tuple_types<tuple,
  497. sizeof...(_Args) < sizeof...(_Tp) ?
  498. sizeof...(_Args) :
  499. sizeof...(_Tp)>::type
  500. >::value &&
  501. __all_default_constructible<
  502. typename __make_tuple_types<tuple, sizeof...(_Tp),
  503. sizeof...(_Args) < sizeof...(_Tp) ?
  504. sizeof...(_Args) :
  505. sizeof...(_Tp)>::type
  506. >::value;
  507. }
  508. };
  509. template <bool _MaybeEnable,
  510. bool = sizeof...(_Tp) == 1,
  511. class _Dummy = void>
  512. struct _CheckTupleLikeConstructor : __check_tuple_constructor_fail {};
  513. template <class _Dummy>
  514. struct _CheckTupleLikeConstructor<true, false, _Dummy>
  515. {
  516. template <class _Tuple>
  517. static constexpr bool __enable_implicit() {
  518. return __tuple_convertible<_Tuple, tuple>::value;
  519. }
  520. template <class _Tuple>
  521. static constexpr bool __enable_explicit() {
  522. return __tuple_constructible<_Tuple, tuple>::value
  523. && !__tuple_convertible<_Tuple, tuple>::value;
  524. }
  525. };
  526. template <class _Dummy>
  527. struct _CheckTupleLikeConstructor<true, true, _Dummy>
  528. {
  529. // This trait is used to disable the tuple-like constructor when
  530. // the UTypes... constructor should be selected instead.
  531. // See LWG issue #2549.
  532. template <class _Tuple>
  533. using _PreferTupleLikeConstructor = __lazy_or<
  534. // Don't attempt the two checks below if the tuple we are given
  535. // has the same type as this tuple.
  536. is_same<typename __uncvref<_Tuple>::type, tuple>,
  537. __lazy_and<
  538. __lazy_not<is_constructible<_Tp..., _Tuple>>,
  539. __lazy_not<is_convertible<_Tuple, _Tp...>>
  540. >
  541. >;
  542. template <class _Tuple>
  543. static constexpr bool __enable_implicit() {
  544. return __lazy_and<
  545. __tuple_convertible<_Tuple, tuple>,
  546. _PreferTupleLikeConstructor<_Tuple>
  547. >::value;
  548. }
  549. template <class _Tuple>
  550. static constexpr bool __enable_explicit() {
  551. return __lazy_and<
  552. __tuple_constructible<_Tuple, tuple>,
  553. _PreferTupleLikeConstructor<_Tuple>,
  554. __lazy_not<__tuple_convertible<_Tuple, tuple>>
  555. >::value;
  556. }
  557. };
  558. template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
  559. typename tuple_element<_Jp, tuple<_Up...> >::type& get(tuple<_Up...>&) _NOEXCEPT;
  560. template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
  561. const typename tuple_element<_Jp, tuple<_Up...> >::type& get(const tuple<_Up...>&) _NOEXCEPT;
  562. template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
  563. typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT;
  564. template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
  565. const typename tuple_element<_Jp, tuple<_Up...> >::type&& get(const tuple<_Up...>&&) _NOEXCEPT;
  566. public:
  567. template <bool _Dummy = true, class = typename enable_if<
  568. __all<__dependent_type<is_default_constructible<_Tp>, _Dummy>::value...>::value
  569. >::type>
  570. _LIBCPP_INLINE_VISIBILITY
  571. _LIBCPP_CONSTEXPR tuple()
  572. _NOEXCEPT_(__all<is_nothrow_default_constructible<_Tp>::value...>::value) {}
  573. template <class _AllocArgT, class _Alloc, bool _Dummy = true, class = typename enable_if<
  574. __lazy_and<
  575. is_same<allocator_arg_t, _AllocArgT>,
  576. __lazy_all<__dependent_type<is_default_constructible<_Tp>, _Dummy>...>
  577. >::value
  578. >::type>
  579. _LIBCPP_INLINE_VISIBILITY
  580. tuple(_AllocArgT, _Alloc const& __a)
  581. : base_(allocator_arg_t(), __a,
  582. __tuple_indices<>(), __tuple_types<>(),
  583. typename __make_tuple_indices<sizeof...(_Tp), 0>::type(),
  584. __tuple_types<_Tp...>()) {}
  585. template <bool _Dummy = true,
  586. typename enable_if
  587. <
  588. _CheckArgsConstructor<
  589. _Dummy
  590. >::template __enable_implicit<_Tp const&...>(),
  591. bool
  592. >::type = false
  593. >
  594. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  595. tuple(const _Tp& ... __t) _NOEXCEPT_((__all<is_nothrow_copy_constructible<_Tp>::value...>::value))
  596. : base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
  597. typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
  598. typename __make_tuple_indices<0>::type(),
  599. typename __make_tuple_types<tuple, 0>::type(),
  600. __t...
  601. ) {}
  602. template <bool _Dummy = true,
  603. typename enable_if
  604. <
  605. _CheckArgsConstructor<
  606. _Dummy
  607. >::template __enable_explicit<_Tp const&...>(),
  608. bool
  609. >::type = false
  610. >
  611. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  612. explicit tuple(const _Tp& ... __t) _NOEXCEPT_((__all<is_nothrow_copy_constructible<_Tp>::value...>::value))
  613. : base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
  614. typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
  615. typename __make_tuple_indices<0>::type(),
  616. typename __make_tuple_types<tuple, 0>::type(),
  617. __t...
  618. ) {}
  619. template <class _Alloc, bool _Dummy = true,
  620. typename enable_if
  621. <
  622. _CheckArgsConstructor<
  623. _Dummy
  624. >::template __enable_implicit<_Tp const&...>(),
  625. bool
  626. >::type = false
  627. >
  628. _LIBCPP_INLINE_VISIBILITY
  629. tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
  630. : base_(allocator_arg_t(), __a,
  631. typename __make_tuple_indices<sizeof...(_Tp)>::type(),
  632. typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
  633. typename __make_tuple_indices<0>::type(),
  634. typename __make_tuple_types<tuple, 0>::type(),
  635. __t...
  636. ) {}
  637. template <class _Alloc, bool _Dummy = true,
  638. typename enable_if
  639. <
  640. _CheckArgsConstructor<
  641. _Dummy
  642. >::template __enable_explicit<_Tp const&...>(),
  643. bool
  644. >::type = false
  645. >
  646. _LIBCPP_INLINE_VISIBILITY
  647. explicit
  648. tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
  649. : base_(allocator_arg_t(), __a,
  650. typename __make_tuple_indices<sizeof...(_Tp)>::type(),
  651. typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
  652. typename __make_tuple_indices<0>::type(),
  653. typename __make_tuple_types<tuple, 0>::type(),
  654. __t...
  655. ) {}
  656. template <class ..._Up,
  657. typename enable_if
  658. <
  659. _CheckArgsConstructor<
  660. sizeof...(_Up) <= sizeof...(_Tp)
  661. && !_PackExpandsToThisTuple<_Up...>::value
  662. >::template __enable_implicit<_Up...>(),
  663. bool
  664. >::type = false
  665. >
  666. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  667. tuple(_Up&&... __u)
  668. _NOEXCEPT_((
  669. is_nothrow_constructible<base,
  670. typename __make_tuple_indices<sizeof...(_Up)>::type,
  671. typename __make_tuple_types<tuple, sizeof...(_Up)>::type,
  672. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type,
  673. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type,
  674. _Up...
  675. >::value
  676. ))
  677. : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
  678. typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
  679. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
  680. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
  681. _VSTD::forward<_Up>(__u)...) {}
  682. template <class ..._Up,
  683. typename enable_if
  684. <
  685. _CheckArgsConstructor<
  686. sizeof...(_Up) <= sizeof...(_Tp)
  687. && !_PackExpandsToThisTuple<_Up...>::value
  688. >::template __enable_explicit<_Up...>(),
  689. bool
  690. >::type = false
  691. >
  692. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  693. explicit
  694. tuple(_Up&&... __u)
  695. _NOEXCEPT_((
  696. is_nothrow_constructible<base,
  697. typename __make_tuple_indices<sizeof...(_Up)>::type,
  698. typename __make_tuple_types<tuple, sizeof...(_Up)>::type,
  699. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type,
  700. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type,
  701. _Up...
  702. >::value
  703. ))
  704. : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
  705. typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
  706. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
  707. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
  708. _VSTD::forward<_Up>(__u)...) {}
  709. template <class _Alloc, class ..._Up,
  710. typename enable_if
  711. <
  712. _CheckArgsConstructor<
  713. sizeof...(_Up) == sizeof...(_Tp) &&
  714. !_PackExpandsToThisTuple<_Up...>::value
  715. >::template __enable_implicit<_Up...>(),
  716. bool
  717. >::type = false
  718. >
  719. _LIBCPP_INLINE_VISIBILITY
  720. tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
  721. : base_(allocator_arg_t(), __a,
  722. typename __make_tuple_indices<sizeof...(_Up)>::type(),
  723. typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
  724. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
  725. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
  726. _VSTD::forward<_Up>(__u)...) {}
  727. template <class _Alloc, class ..._Up,
  728. typename enable_if
  729. <
  730. _CheckArgsConstructor<
  731. sizeof...(_Up) == sizeof...(_Tp) &&
  732. !_PackExpandsToThisTuple<_Up...>::value
  733. >::template __enable_explicit<_Up...>(),
  734. bool
  735. >::type = false
  736. >
  737. _LIBCPP_INLINE_VISIBILITY
  738. explicit
  739. tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
  740. : base_(allocator_arg_t(), __a,
  741. typename __make_tuple_indices<sizeof...(_Up)>::type(),
  742. typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
  743. typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
  744. typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
  745. _VSTD::forward<_Up>(__u)...) {}
  746. template <class _Tuple,
  747. typename enable_if
  748. <
  749. _CheckTupleLikeConstructor<
  750. __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
  751. && !_PackExpandsToThisTuple<_Tuple>::value
  752. >::template __enable_implicit<_Tuple>(),
  753. bool
  754. >::type = false
  755. >
  756. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  757. tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _Tuple>::value))
  758. : base_(_VSTD::forward<_Tuple>(__t)) {}
  759. template <class _Tuple,
  760. typename enable_if
  761. <
  762. _CheckTupleLikeConstructor<
  763. __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
  764. && !_PackExpandsToThisTuple<_Tuple>::value
  765. >::template __enable_explicit<_Tuple>(),
  766. bool
  767. >::type = false
  768. >
  769. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  770. explicit
  771. tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _Tuple>::value))
  772. : base_(_VSTD::forward<_Tuple>(__t)) {}
  773. template <class _Alloc, class _Tuple,
  774. typename enable_if
  775. <
  776. _CheckTupleLikeConstructor<
  777. __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
  778. >::template __enable_implicit<_Tuple>(),
  779. bool
  780. >::type = false
  781. >
  782. _LIBCPP_INLINE_VISIBILITY
  783. tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
  784. : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {}
  785. template <class _Alloc, class _Tuple,
  786. typename enable_if
  787. <
  788. _CheckTupleLikeConstructor<
  789. __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
  790. >::template __enable_explicit<_Tuple>(),
  791. bool
  792. >::type = false
  793. >
  794. _LIBCPP_INLINE_VISIBILITY
  795. explicit
  796. tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
  797. : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {}
  798. template <class _Tuple,
  799. class = typename enable_if
  800. <
  801. __tuple_assignable<_Tuple, tuple>::value
  802. >::type
  803. >
  804. _LIBCPP_INLINE_VISIBILITY
  805. tuple&
  806. operator=(_Tuple&& __t) _NOEXCEPT_((is_nothrow_assignable<base&, _Tuple>::value))
  807. {
  808. base_.operator=(_VSTD::forward<_Tuple>(__t));
  809. return *this;
  810. }
  811. _LIBCPP_INLINE_VISIBILITY
  812. void swap(tuple& __t) _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
  813. {base_.swap(__t.base_);}
  814. };
  815. template <>
  816. class _LIBCPP_TYPE_VIS_ONLY tuple<>
  817. {
  818. public:
  819. _LIBCPP_INLINE_VISIBILITY
  820. _LIBCPP_CONSTEXPR tuple() _NOEXCEPT {}
  821. template <class _Alloc>
  822. _LIBCPP_INLINE_VISIBILITY
  823. tuple(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
  824. template <class _Alloc>
  825. _LIBCPP_INLINE_VISIBILITY
  826. tuple(allocator_arg_t, const _Alloc&, const tuple&) _NOEXCEPT {}
  827. template <class _Up>
  828. _LIBCPP_INLINE_VISIBILITY
  829. tuple(array<_Up, 0>) _NOEXCEPT {}
  830. template <class _Alloc, class _Up>
  831. _LIBCPP_INLINE_VISIBILITY
  832. tuple(allocator_arg_t, const _Alloc&, array<_Up, 0>) _NOEXCEPT {}
  833. _LIBCPP_INLINE_VISIBILITY
  834. void swap(tuple&) _NOEXCEPT {}
  835. };
  836. template <class ..._Tp>
  837. inline _LIBCPP_INLINE_VISIBILITY
  838. typename enable_if
  839. <
  840. __all<__is_swappable<_Tp>::value...>::value,
  841. void
  842. >::type
  843. swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
  844. _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
  845. {__t.swap(__u);}
  846. // get
  847. template <size_t _Ip, class ..._Tp>
  848. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  849. typename tuple_element<_Ip, tuple<_Tp...> >::type&
  850. get(tuple<_Tp...>& __t) _NOEXCEPT
  851. {
  852. typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
  853. return static_cast<__tuple_leaf<_Ip, type>&>(__t.base_).get();
  854. }
  855. template <size_t _Ip, class ..._Tp>
  856. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  857. const typename tuple_element<_Ip, tuple<_Tp...> >::type&
  858. get(const tuple<_Tp...>& __t) _NOEXCEPT
  859. {
  860. typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
  861. return static_cast<const __tuple_leaf<_Ip, type>&>(__t.base_).get();
  862. }
  863. template <size_t _Ip, class ..._Tp>
  864. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  865. typename tuple_element<_Ip, tuple<_Tp...> >::type&&
  866. get(tuple<_Tp...>&& __t) _NOEXCEPT
  867. {
  868. typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
  869. return static_cast<type&&>(
  870. static_cast<__tuple_leaf<_Ip, type>&&>(__t.base_).get());
  871. }
  872. template <size_t _Ip, class ..._Tp>
  873. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  874. const typename tuple_element<_Ip, tuple<_Tp...> >::type&&
  875. get(const tuple<_Tp...>&& __t) _NOEXCEPT
  876. {
  877. typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
  878. return static_cast<const type&&>(
  879. static_cast<const __tuple_leaf<_Ip, type>&&>(__t.base_).get());
  880. }
  881. #if _LIBCPP_STD_VER > 11
  882. namespace __find_detail {
  883. static constexpr size_t __not_found = -1;
  884. static constexpr size_t __ambiguous = __not_found - 1;
  885. inline _LIBCPP_INLINE_VISIBILITY
  886. constexpr size_t __find_idx_return(size_t __curr_i, size_t __res, bool __matches) {
  887. return !__matches ? __res :
  888. (__res == __not_found ? __curr_i : __ambiguous);
  889. }
  890. template <size_t _Nx>
  891. inline _LIBCPP_INLINE_VISIBILITY
  892. constexpr size_t __find_idx(size_t __i, const bool (&__matches)[_Nx]) {
  893. return __i == _Nx ? __not_found :
  894. __find_idx_return(__i, __find_idx(__i + 1, __matches), __matches[__i]);
  895. }
  896. template <class _T1, class ..._Args>
  897. struct __find_exactly_one_checked {
  898. static constexpr bool __matches[] = {is_same<_T1, _Args>::value...};
  899. static constexpr size_t value = __find_detail::__find_idx(0, __matches);
  900. static_assert (value != __not_found, "type not found in type list" );
  901. static_assert(value != __ambiguous,"type occurs more than once in type list");
  902. };
  903. template <class _T1>
  904. struct __find_exactly_one_checked<_T1> {
  905. static_assert(!is_same<_T1, _T1>::value, "type not in empty type list");
  906. };
  907. } // namespace __find_detail;
  908. template <typename _T1, typename... _Args>
  909. struct __find_exactly_one_t
  910. : public __find_detail::__find_exactly_one_checked<_T1, _Args...> {
  911. };
  912. template <class _T1, class... _Args>
  913. inline _LIBCPP_INLINE_VISIBILITY
  914. constexpr _T1& get(tuple<_Args...>& __tup) noexcept
  915. {
  916. return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
  917. }
  918. template <class _T1, class... _Args>
  919. inline _LIBCPP_INLINE_VISIBILITY
  920. constexpr _T1 const& get(tuple<_Args...> const& __tup) noexcept
  921. {
  922. return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
  923. }
  924. template <class _T1, class... _Args>
  925. inline _LIBCPP_INLINE_VISIBILITY
  926. constexpr _T1&& get(tuple<_Args...>&& __tup) noexcept
  927. {
  928. return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(_VSTD::move(__tup));
  929. }
  930. template <class _T1, class... _Args>
  931. inline _LIBCPP_INLINE_VISIBILITY
  932. constexpr _T1 const&& get(tuple<_Args...> const&& __tup) noexcept
  933. {
  934. return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(_VSTD::move(__tup));
  935. }
  936. #endif
  937. // tie
  938. template <class ..._Tp>
  939. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  940. tuple<_Tp&...>
  941. tie(_Tp&... __t) _NOEXCEPT
  942. {
  943. return tuple<_Tp&...>(__t...);
  944. }
  945. template <class _Up>
  946. struct __ignore_t
  947. {
  948. template <class _Tp>
  949. _LIBCPP_INLINE_VISIBILITY
  950. const __ignore_t& operator=(_Tp&&) const {return *this;}
  951. };
  952. namespace { const __ignore_t<unsigned char> ignore = __ignore_t<unsigned char>(); }
  953. template <class _Tp>
  954. struct __make_tuple_return_impl
  955. {
  956. typedef _Tp type;
  957. };
  958. template <class _Tp>
  959. struct __make_tuple_return_impl<reference_wrapper<_Tp> >
  960. {
  961. typedef _Tp& type;
  962. };
  963. template <class _Tp>
  964. struct __make_tuple_return
  965. {
  966. typedef typename __make_tuple_return_impl<typename decay<_Tp>::type>::type type;
  967. };
  968. template <class... _Tp>
  969. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  970. tuple<typename __make_tuple_return<_Tp>::type...>
  971. make_tuple(_Tp&&... __t)
  972. {
  973. return tuple<typename __make_tuple_return<_Tp>::type...>(_VSTD::forward<_Tp>(__t)...);
  974. }
  975. template <class... _Tp>
  976. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  977. tuple<_Tp&&...>
  978. forward_as_tuple(_Tp&&... __t) _NOEXCEPT
  979. {
  980. return tuple<_Tp&&...>(_VSTD::forward<_Tp>(__t)...);
  981. }
  982. template <size_t _Ip>
  983. struct __tuple_equal
  984. {
  985. template <class _Tp, class _Up>
  986. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  987. bool operator()(const _Tp& __x, const _Up& __y)
  988. {
  989. return __tuple_equal<_Ip - 1>()(__x, __y) && _VSTD::get<_Ip-1>(__x) == _VSTD::get<_Ip-1>(__y);
  990. }
  991. };
  992. template <>
  993. struct __tuple_equal<0>
  994. {
  995. template <class _Tp, class _Up>
  996. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  997. bool operator()(const _Tp&, const _Up&)
  998. {
  999. return true;
  1000. }
  1001. };
  1002. template <class ..._Tp, class ..._Up>
  1003. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1004. bool
  1005. operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1006. {
  1007. return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
  1008. }
  1009. template <class ..._Tp, class ..._Up>
  1010. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1011. bool
  1012. operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1013. {
  1014. return !(__x == __y);
  1015. }
  1016. template <size_t _Ip>
  1017. struct __tuple_less
  1018. {
  1019. template <class _Tp, class _Up>
  1020. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1021. bool operator()(const _Tp& __x, const _Up& __y)
  1022. {
  1023. const size_t __idx = tuple_size<_Tp>::value - _Ip;
  1024. if (_VSTD::get<__idx>(__x) < _VSTD::get<__idx>(__y))
  1025. return true;
  1026. if (_VSTD::get<__idx>(__y) < _VSTD::get<__idx>(__x))
  1027. return false;
  1028. return __tuple_less<_Ip-1>()(__x, __y);
  1029. }
  1030. };
  1031. template <>
  1032. struct __tuple_less<0>
  1033. {
  1034. template <class _Tp, class _Up>
  1035. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1036. bool operator()(const _Tp&, const _Up&)
  1037. {
  1038. return false;
  1039. }
  1040. };
  1041. template <class ..._Tp, class ..._Up>
  1042. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1043. bool
  1044. operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1045. {
  1046. return __tuple_less<sizeof...(_Tp)>()(__x, __y);
  1047. }
  1048. template <class ..._Tp, class ..._Up>
  1049. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1050. bool
  1051. operator>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1052. {
  1053. return __y < __x;
  1054. }
  1055. template <class ..._Tp, class ..._Up>
  1056. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1057. bool
  1058. operator>=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1059. {
  1060. return !(__x < __y);
  1061. }
  1062. template <class ..._Tp, class ..._Up>
  1063. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1064. bool
  1065. operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
  1066. {
  1067. return !(__y < __x);
  1068. }
  1069. // tuple_cat
  1070. template <class _Tp, class _Up> struct __tuple_cat_type;
  1071. template <class ..._Ttypes, class ..._Utypes>
  1072. struct __tuple_cat_type<tuple<_Ttypes...>, __tuple_types<_Utypes...> >
  1073. {
  1074. typedef tuple<_Ttypes..., _Utypes...> type;
  1075. };
  1076. template <class _ResultTuple, bool _Is_Tuple0TupleLike, class ..._Tuples>
  1077. struct __tuple_cat_return_1
  1078. {
  1079. };
  1080. template <class ..._Types, class _Tuple0>
  1081. struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0>
  1082. {
  1083. typedef typename __tuple_cat_type<tuple<_Types...>,
  1084. typename __make_tuple_types<typename remove_reference<_Tuple0>::type>::type>::type
  1085. type;
  1086. };
  1087. template <class ..._Types, class _Tuple0, class _Tuple1, class ..._Tuples>
  1088. struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0, _Tuple1, _Tuples...>
  1089. : public __tuple_cat_return_1<
  1090. typename __tuple_cat_type<
  1091. tuple<_Types...>,
  1092. typename __make_tuple_types<typename remove_reference<_Tuple0>::type>::type
  1093. >::type,
  1094. __tuple_like<typename remove_reference<_Tuple1>::type>::value,
  1095. _Tuple1, _Tuples...>
  1096. {
  1097. };
  1098. template <class ..._Tuples> struct __tuple_cat_return;
  1099. template <class _Tuple0, class ..._Tuples>
  1100. struct __tuple_cat_return<_Tuple0, _Tuples...>
  1101. : public __tuple_cat_return_1<tuple<>,
  1102. __tuple_like<typename remove_reference<_Tuple0>::type>::value, _Tuple0,
  1103. _Tuples...>
  1104. {
  1105. };
  1106. template <>
  1107. struct __tuple_cat_return<>
  1108. {
  1109. typedef tuple<> type;
  1110. };
  1111. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1112. tuple<>
  1113. tuple_cat()
  1114. {
  1115. return tuple<>();
  1116. }
  1117. template <class _Rp, class _Indices, class _Tuple0, class ..._Tuples>
  1118. struct __tuple_cat_return_ref_imp;
  1119. template <class ..._Types, size_t ..._I0, class _Tuple0>
  1120. struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>, _Tuple0>
  1121. {
  1122. typedef typename remove_reference<_Tuple0>::type _T0;
  1123. typedef tuple<_Types..., typename __apply_cv<_Tuple0,
  1124. typename tuple_element<_I0, _T0>::type>::type&&...> type;
  1125. };
  1126. template <class ..._Types, size_t ..._I0, class _Tuple0, class _Tuple1, class ..._Tuples>
  1127. struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>,
  1128. _Tuple0, _Tuple1, _Tuples...>
  1129. : public __tuple_cat_return_ref_imp<
  1130. tuple<_Types..., typename __apply_cv<_Tuple0,
  1131. typename tuple_element<_I0,
  1132. typename remove_reference<_Tuple0>::type>::type>::type&&...>,
  1133. typename __make_tuple_indices<tuple_size<typename
  1134. remove_reference<_Tuple1>::type>::value>::type,
  1135. _Tuple1, _Tuples...>
  1136. {
  1137. };
  1138. template <class _Tuple0, class ..._Tuples>
  1139. struct __tuple_cat_return_ref
  1140. : public __tuple_cat_return_ref_imp<tuple<>,
  1141. typename __make_tuple_indices<
  1142. tuple_size<typename remove_reference<_Tuple0>::type>::value
  1143. >::type, _Tuple0, _Tuples...>
  1144. {
  1145. };
  1146. template <class _Types, class _I0, class _J0>
  1147. struct __tuple_cat;
  1148. template <class ..._Types, size_t ..._I0, size_t ..._J0>
  1149. struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J0...> >
  1150. {
  1151. template <class _Tuple0>
  1152. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1153. typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&>::type
  1154. operator()(tuple<_Types...> __t, _Tuple0&& __t0)
  1155. {
  1156. return forward_as_tuple(_VSTD::forward<_Types>(_VSTD::get<_I0>(__t))...,
  1157. _VSTD::get<_J0>(_VSTD::forward<_Tuple0>(__t0))...);
  1158. }
  1159. template <class _Tuple0, class _Tuple1, class ..._Tuples>
  1160. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1161. typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&, _Tuple1&&, _Tuples&&...>::type
  1162. operator()(tuple<_Types...> __t, _Tuple0&& __t0, _Tuple1&& __t1, _Tuples&& ...__tpls)
  1163. {
  1164. typedef typename remove_reference<_Tuple0>::type _T0;
  1165. typedef typename remove_reference<_Tuple1>::type _T1;
  1166. return __tuple_cat<
  1167. tuple<_Types..., typename __apply_cv<_Tuple0, typename tuple_element<_J0, _T0>::type>::type&&...>,
  1168. typename __make_tuple_indices<sizeof ...(_Types) + tuple_size<_T0>::value>::type,
  1169. typename __make_tuple_indices<tuple_size<_T1>::value>::type>()
  1170. (forward_as_tuple(
  1171. _VSTD::forward<_Types>(_VSTD::get<_I0>(__t))...,
  1172. _VSTD::get<_J0>(_VSTD::forward<_Tuple0>(__t0))...
  1173. ),
  1174. _VSTD::forward<_Tuple1>(__t1),
  1175. _VSTD::forward<_Tuples>(__tpls)...);
  1176. }
  1177. };
  1178. template <class _Tuple0, class... _Tuples>
  1179. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1180. typename __tuple_cat_return<_Tuple0, _Tuples...>::type
  1181. tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
  1182. {
  1183. typedef typename remove_reference<_Tuple0>::type _T0;
  1184. return __tuple_cat<tuple<>, __tuple_indices<>,
  1185. typename __make_tuple_indices<tuple_size<_T0>::value>::type>()
  1186. (tuple<>(), _VSTD::forward<_Tuple0>(__t0),
  1187. _VSTD::forward<_Tuples>(__tpls)...);
  1188. }
  1189. template <class ..._Tp, class _Alloc>
  1190. struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<tuple<_Tp...>, _Alloc>
  1191. : true_type {};
  1192. template <class _T1, class _T2>
  1193. template <class... _Args1, class... _Args2, size_t ..._I1, size_t ..._I2>
  1194. inline _LIBCPP_INLINE_VISIBILITY
  1195. pair<_T1, _T2>::pair(piecewise_construct_t,
  1196. tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
  1197. __tuple_indices<_I1...>, __tuple_indices<_I2...>)
  1198. : first(_VSTD::forward<_Args1>(_VSTD::get<_I1>( __first_args))...),
  1199. second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
  1200. {
  1201. }
  1202. #endif // _LIBCPP_HAS_NO_VARIADICS
  1203. #if _LIBCPP_STD_VER > 14
  1204. template <class _Tp>
  1205. constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
  1206. #define _LIBCPP_NOEXCEPT_RETURN(...) noexcept(noexcept(__VA_ARGS__)) { return __VA_ARGS__; }
  1207. template <class _Fn, class _Tuple, size_t ..._Id>
  1208. inline _LIBCPP_INLINE_VISIBILITY
  1209. constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
  1210. __tuple_indices<_Id...>)
  1211. _LIBCPP_NOEXCEPT_RETURN(
  1212. _VSTD::__invoke_constexpr(
  1213. _VSTD::forward<_Fn>(__f),
  1214. _VSTD::get<_Id>(_VSTD::forward<_Tuple>(__t))...)
  1215. )
  1216. template <class _Fn, class _Tuple>
  1217. inline _LIBCPP_INLINE_VISIBILITY
  1218. constexpr decltype(auto) apply(_Fn && __f, _Tuple && __t)
  1219. _LIBCPP_NOEXCEPT_RETURN(
  1220. _VSTD::__apply_tuple_impl(
  1221. _VSTD::forward<_Fn>(__f), _VSTD::forward<_Tuple>(__t),
  1222. typename __make_tuple_indices<tuple_size_v<decay_t<_Tuple>>>::type{})
  1223. )
  1224. template <class _Tp, class _Tuple, size_t... _Idx>
  1225. inline _LIBCPP_INLINE_VISIBILITY
  1226. constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>)
  1227. _LIBCPP_NOEXCEPT_RETURN(
  1228. _Tp(_VSTD::get<_Idx>(_VSTD::forward<_Tuple>(__t))...)
  1229. )
  1230. template <class _Tp, class _Tuple>
  1231. inline _LIBCPP_INLINE_VISIBILITY
  1232. constexpr _Tp make_from_tuple(_Tuple&& __t)
  1233. _LIBCPP_NOEXCEPT_RETURN(
  1234. _VSTD::__make_from_tuple_impl<_Tp>(_VSTD::forward<_Tuple>(__t),
  1235. typename __make_tuple_indices<tuple_size_v<decay_t<_Tuple>>>::type{})
  1236. )
  1237. #undef _LIBCPP_NOEXCEPT_RETURN
  1238. #endif // _LIBCPP_STD_VER > 14
  1239. _LIBCPP_END_NAMESPACE_STD
  1240. #endif // _LIBCPP_TUPLE