tuple 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. // <tuple> -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/tuple
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_TUPLE
  24. #define _GLIBCXX_TUPLE 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <bits/stl_pair.h> // for std::pair
  30. #include <bits/uses_allocator.h> // for std::allocator_arg_t
  31. #include <bits/utility.h> // for std::get, std::tuple_size etc.
  32. #include <bits/invoke.h> // for std::__invoke
  33. #if __cplusplus > 201703L
  34. # include <compare>
  35. # define __cpp_lib_constexpr_tuple 201811L
  36. #endif
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. /**
  41. * @addtogroup utilities
  42. * @{
  43. */
  44. template<typename... _Elements>
  45. class tuple;
  46. template<typename _Tp>
  47. struct __is_empty_non_tuple : is_empty<_Tp> { };
  48. // Using EBO for elements that are tuples causes ambiguous base errors.
  49. template<typename _El0, typename... _El>
  50. struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
  51. // Use the Empty Base-class Optimization for empty, non-final types.
  52. template<typename _Tp>
  53. using __empty_not_final
  54. = __conditional_t<__is_final(_Tp), false_type,
  55. __is_empty_non_tuple<_Tp>>;
  56. template<size_t _Idx, typename _Head,
  57. bool = __empty_not_final<_Head>::value>
  58. struct _Head_base;
  59. #if __has_cpp_attribute(__no_unique_address__)
  60. template<size_t _Idx, typename _Head>
  61. struct _Head_base<_Idx, _Head, true>
  62. {
  63. constexpr _Head_base()
  64. : _M_head_impl() { }
  65. constexpr _Head_base(const _Head& __h)
  66. : _M_head_impl(__h) { }
  67. constexpr _Head_base(const _Head_base&) = default;
  68. constexpr _Head_base(_Head_base&&) = default;
  69. template<typename _UHead>
  70. constexpr _Head_base(_UHead&& __h)
  71. : _M_head_impl(std::forward<_UHead>(__h)) { }
  72. _GLIBCXX20_CONSTEXPR
  73. _Head_base(allocator_arg_t, __uses_alloc0)
  74. : _M_head_impl() { }
  75. template<typename _Alloc>
  76. _GLIBCXX20_CONSTEXPR
  77. _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
  78. : _M_head_impl(allocator_arg, *__a._M_a) { }
  79. template<typename _Alloc>
  80. _GLIBCXX20_CONSTEXPR
  81. _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
  82. : _M_head_impl(*__a._M_a) { }
  83. template<typename _UHead>
  84. _GLIBCXX20_CONSTEXPR
  85. _Head_base(__uses_alloc0, _UHead&& __uhead)
  86. : _M_head_impl(std::forward<_UHead>(__uhead)) { }
  87. template<typename _Alloc, typename _UHead>
  88. _GLIBCXX20_CONSTEXPR
  89. _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
  90. : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
  91. { }
  92. template<typename _Alloc, typename _UHead>
  93. _GLIBCXX20_CONSTEXPR
  94. _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
  95. : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
  96. static constexpr _Head&
  97. _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
  98. static constexpr const _Head&
  99. _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
  100. [[__no_unique_address__]] _Head _M_head_impl;
  101. };
  102. #else
  103. template<size_t _Idx, typename _Head>
  104. struct _Head_base<_Idx, _Head, true>
  105. : public _Head
  106. {
  107. constexpr _Head_base()
  108. : _Head() { }
  109. constexpr _Head_base(const _Head& __h)
  110. : _Head(__h) { }
  111. constexpr _Head_base(const _Head_base&) = default;
  112. constexpr _Head_base(_Head_base&&) = default;
  113. template<typename _UHead>
  114. constexpr _Head_base(_UHead&& __h)
  115. : _Head(std::forward<_UHead>(__h)) { }
  116. _GLIBCXX20_CONSTEXPR
  117. _Head_base(allocator_arg_t, __uses_alloc0)
  118. : _Head() { }
  119. template<typename _Alloc>
  120. _GLIBCXX20_CONSTEXPR
  121. _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
  122. : _Head(allocator_arg, *__a._M_a) { }
  123. template<typename _Alloc>
  124. _GLIBCXX20_CONSTEXPR
  125. _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
  126. : _Head(*__a._M_a) { }
  127. template<typename _UHead>
  128. _GLIBCXX20_CONSTEXPR
  129. _Head_base(__uses_alloc0, _UHead&& __uhead)
  130. : _Head(std::forward<_UHead>(__uhead)) { }
  131. template<typename _Alloc, typename _UHead>
  132. _GLIBCXX20_CONSTEXPR
  133. _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
  134. : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
  135. template<typename _Alloc, typename _UHead>
  136. _GLIBCXX20_CONSTEXPR
  137. _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
  138. : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
  139. static constexpr _Head&
  140. _M_head(_Head_base& __b) noexcept { return __b; }
  141. static constexpr const _Head&
  142. _M_head(const _Head_base& __b) noexcept { return __b; }
  143. };
  144. #endif
  145. template<size_t _Idx, typename _Head>
  146. struct _Head_base<_Idx, _Head, false>
  147. {
  148. constexpr _Head_base()
  149. : _M_head_impl() { }
  150. constexpr _Head_base(const _Head& __h)
  151. : _M_head_impl(__h) { }
  152. constexpr _Head_base(const _Head_base&) = default;
  153. constexpr _Head_base(_Head_base&&) = default;
  154. template<typename _UHead>
  155. constexpr _Head_base(_UHead&& __h)
  156. : _M_head_impl(std::forward<_UHead>(__h)) { }
  157. _GLIBCXX20_CONSTEXPR
  158. _Head_base(allocator_arg_t, __uses_alloc0)
  159. : _M_head_impl() { }
  160. template<typename _Alloc>
  161. _GLIBCXX20_CONSTEXPR
  162. _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
  163. : _M_head_impl(allocator_arg, *__a._M_a) { }
  164. template<typename _Alloc>
  165. _GLIBCXX20_CONSTEXPR
  166. _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
  167. : _M_head_impl(*__a._M_a) { }
  168. template<typename _UHead>
  169. _GLIBCXX20_CONSTEXPR
  170. _Head_base(__uses_alloc0, _UHead&& __uhead)
  171. : _M_head_impl(std::forward<_UHead>(__uhead)) { }
  172. template<typename _Alloc, typename _UHead>
  173. _GLIBCXX20_CONSTEXPR
  174. _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
  175. : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
  176. { }
  177. template<typename _Alloc, typename _UHead>
  178. _GLIBCXX20_CONSTEXPR
  179. _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
  180. : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
  181. static constexpr _Head&
  182. _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
  183. static constexpr const _Head&
  184. _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
  185. _Head _M_head_impl;
  186. };
  187. /**
  188. * Contains the actual implementation of the @c tuple template, stored
  189. * as a recursive inheritance hierarchy from the first element (most
  190. * derived class) to the last (least derived class). The @c Idx
  191. * parameter gives the 0-based index of the element stored at this
  192. * point in the hierarchy; we use it to implement a constant-time
  193. * get() operation.
  194. */
  195. template<size_t _Idx, typename... _Elements>
  196. struct _Tuple_impl;
  197. /**
  198. * Recursive tuple implementation. Here we store the @c Head element
  199. * and derive from a @c Tuple_impl containing the remaining elements
  200. * (which contains the @c Tail).
  201. */
  202. template<size_t _Idx, typename _Head, typename... _Tail>
  203. struct _Tuple_impl<_Idx, _Head, _Tail...>
  204. : public _Tuple_impl<_Idx + 1, _Tail...>,
  205. private _Head_base<_Idx, _Head>
  206. {
  207. template<size_t, typename...> friend struct _Tuple_impl;
  208. typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
  209. typedef _Head_base<_Idx, _Head> _Base;
  210. static constexpr _Head&
  211. _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  212. static constexpr const _Head&
  213. _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  214. static constexpr _Inherited&
  215. _M_tail(_Tuple_impl& __t) noexcept { return __t; }
  216. static constexpr const _Inherited&
  217. _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
  218. constexpr _Tuple_impl()
  219. : _Inherited(), _Base() { }
  220. explicit constexpr
  221. _Tuple_impl(const _Head& __head, const _Tail&... __tail)
  222. : _Inherited(__tail...), _Base(__head)
  223. { }
  224. template<typename _UHead, typename... _UTail,
  225. typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
  226. explicit constexpr
  227. _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
  228. : _Inherited(std::forward<_UTail>(__tail)...),
  229. _Base(std::forward<_UHead>(__head))
  230. { }
  231. constexpr _Tuple_impl(const _Tuple_impl&) = default;
  232. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  233. // 2729. Missing SFINAE on std::pair::operator=
  234. _Tuple_impl& operator=(const _Tuple_impl&) = delete;
  235. _Tuple_impl(_Tuple_impl&&) = default;
  236. template<typename... _UElements>
  237. constexpr
  238. _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
  239. : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
  240. _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
  241. { }
  242. template<typename _UHead, typename... _UTails>
  243. constexpr
  244. _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  245. : _Inherited(std::move
  246. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
  247. _Base(std::forward<_UHead>
  248. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
  249. { }
  250. template<typename _Alloc>
  251. _GLIBCXX20_CONSTEXPR
  252. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
  253. : _Inherited(__tag, __a),
  254. _Base(__tag, __use_alloc<_Head>(__a))
  255. { }
  256. template<typename _Alloc>
  257. _GLIBCXX20_CONSTEXPR
  258. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  259. const _Head& __head, const _Tail&... __tail)
  260. : _Inherited(__tag, __a, __tail...),
  261. _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head)
  262. { }
  263. template<typename _Alloc, typename _UHead, typename... _UTail,
  264. typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
  265. _GLIBCXX20_CONSTEXPR
  266. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  267. _UHead&& __head, _UTail&&... __tail)
  268. : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
  269. _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  270. std::forward<_UHead>(__head))
  271. { }
  272. template<typename _Alloc>
  273. _GLIBCXX20_CONSTEXPR
  274. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  275. const _Tuple_impl& __in)
  276. : _Inherited(__tag, __a, _M_tail(__in)),
  277. _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in))
  278. { }
  279. template<typename _Alloc>
  280. _GLIBCXX20_CONSTEXPR
  281. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  282. _Tuple_impl&& __in)
  283. : _Inherited(__tag, __a, std::move(_M_tail(__in))),
  284. _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  285. std::forward<_Head>(_M_head(__in)))
  286. { }
  287. template<typename _Alloc, typename _UHead, typename... _UTails>
  288. _GLIBCXX20_CONSTEXPR
  289. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  290. const _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
  291. : _Inherited(__tag, __a,
  292. _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
  293. _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
  294. _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
  295. { }
  296. template<typename _Alloc, typename _UHead, typename... _UTails>
  297. _GLIBCXX20_CONSTEXPR
  298. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  299. _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  300. : _Inherited(__tag, __a, std::move
  301. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
  302. _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  303. std::forward<_UHead>
  304. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
  305. { }
  306. template<typename... _UElements>
  307. _GLIBCXX20_CONSTEXPR
  308. void
  309. _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
  310. {
  311. _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
  312. _M_tail(*this)._M_assign(
  313. _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
  314. }
  315. template<typename _UHead, typename... _UTails>
  316. _GLIBCXX20_CONSTEXPR
  317. void
  318. _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  319. {
  320. _M_head(*this) = std::forward<_UHead>
  321. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
  322. _M_tail(*this)._M_assign(
  323. std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
  324. }
  325. protected:
  326. _GLIBCXX20_CONSTEXPR
  327. void
  328. _M_swap(_Tuple_impl& __in)
  329. {
  330. using std::swap;
  331. swap(_M_head(*this), _M_head(__in));
  332. _Inherited::_M_swap(_M_tail(__in));
  333. }
  334. };
  335. // Basis case of inheritance recursion.
  336. template<size_t _Idx, typename _Head>
  337. struct _Tuple_impl<_Idx, _Head>
  338. : private _Head_base<_Idx, _Head>
  339. {
  340. template<size_t, typename...> friend struct _Tuple_impl;
  341. typedef _Head_base<_Idx, _Head> _Base;
  342. static constexpr _Head&
  343. _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  344. static constexpr const _Head&
  345. _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  346. constexpr
  347. _Tuple_impl()
  348. : _Base() { }
  349. explicit constexpr
  350. _Tuple_impl(const _Head& __head)
  351. : _Base(__head)
  352. { }
  353. template<typename _UHead>
  354. explicit constexpr
  355. _Tuple_impl(_UHead&& __head)
  356. : _Base(std::forward<_UHead>(__head))
  357. { }
  358. constexpr _Tuple_impl(const _Tuple_impl&) = default;
  359. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  360. // 2729. Missing SFINAE on std::pair::operator=
  361. _Tuple_impl& operator=(const _Tuple_impl&) = delete;
  362. #if _GLIBCXX_INLINE_VERSION
  363. _Tuple_impl(_Tuple_impl&&) = default;
  364. #else
  365. constexpr
  366. _Tuple_impl(_Tuple_impl&& __in)
  367. noexcept(is_nothrow_move_constructible<_Head>::value)
  368. : _Base(static_cast<_Base&&>(__in))
  369. { }
  370. #endif
  371. template<typename _UHead>
  372. constexpr
  373. _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
  374. : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
  375. { }
  376. template<typename _UHead>
  377. constexpr
  378. _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
  379. : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
  380. { }
  381. template<typename _Alloc>
  382. _GLIBCXX20_CONSTEXPR
  383. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
  384. : _Base(__tag, __use_alloc<_Head>(__a))
  385. { }
  386. template<typename _Alloc>
  387. _GLIBCXX20_CONSTEXPR
  388. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  389. const _Head& __head)
  390. : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), __head)
  391. { }
  392. template<typename _Alloc, typename _UHead>
  393. _GLIBCXX20_CONSTEXPR
  394. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  395. _UHead&& __head)
  396. : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  397. std::forward<_UHead>(__head))
  398. { }
  399. template<typename _Alloc>
  400. _GLIBCXX20_CONSTEXPR
  401. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  402. const _Tuple_impl& __in)
  403. : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), _M_head(__in))
  404. { }
  405. template<typename _Alloc>
  406. _GLIBCXX20_CONSTEXPR
  407. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  408. _Tuple_impl&& __in)
  409. : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  410. std::forward<_Head>(_M_head(__in)))
  411. { }
  412. template<typename _Alloc, typename _UHead>
  413. _GLIBCXX20_CONSTEXPR
  414. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  415. const _Tuple_impl<_Idx, _UHead>& __in)
  416. : _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
  417. _Tuple_impl<_Idx, _UHead>::_M_head(__in))
  418. { }
  419. template<typename _Alloc, typename _UHead>
  420. _GLIBCXX20_CONSTEXPR
  421. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  422. _Tuple_impl<_Idx, _UHead>&& __in)
  423. : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  424. std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
  425. { }
  426. template<typename _UHead>
  427. _GLIBCXX20_CONSTEXPR
  428. void
  429. _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
  430. {
  431. _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
  432. }
  433. template<typename _UHead>
  434. _GLIBCXX20_CONSTEXPR
  435. void
  436. _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
  437. {
  438. _M_head(*this)
  439. = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
  440. }
  441. protected:
  442. _GLIBCXX20_CONSTEXPR
  443. void
  444. _M_swap(_Tuple_impl& __in)
  445. {
  446. using std::swap;
  447. swap(_M_head(*this), _M_head(__in));
  448. }
  449. };
  450. // Concept utility functions, reused in conditionally-explicit
  451. // constructors.
  452. template<bool, typename... _Types>
  453. struct _TupleConstraints
  454. {
  455. // Constraint for a non-explicit constructor.
  456. // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
  457. // and every Ui is implicitly convertible to Ti.
  458. template<typename... _UTypes>
  459. static constexpr bool __is_implicitly_constructible()
  460. {
  461. return __and_<is_constructible<_Types, _UTypes>...,
  462. is_convertible<_UTypes, _Types>...
  463. >::value;
  464. }
  465. // Constraint for a non-explicit constructor.
  466. // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
  467. // but not every Ui is implicitly convertible to Ti.
  468. template<typename... _UTypes>
  469. static constexpr bool __is_explicitly_constructible()
  470. {
  471. return __and_<is_constructible<_Types, _UTypes>...,
  472. __not_<__and_<is_convertible<_UTypes, _Types>...>>
  473. >::value;
  474. }
  475. static constexpr bool __is_implicitly_default_constructible()
  476. {
  477. return __and_<std::__is_implicitly_default_constructible<_Types>...
  478. >::value;
  479. }
  480. static constexpr bool __is_explicitly_default_constructible()
  481. {
  482. return __and_<is_default_constructible<_Types>...,
  483. __not_<__and_<
  484. std::__is_implicitly_default_constructible<_Types>...>
  485. >>::value;
  486. }
  487. };
  488. // Partial specialization used when a required precondition isn't met,
  489. // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
  490. template<typename... _Types>
  491. struct _TupleConstraints<false, _Types...>
  492. {
  493. template<typename... _UTypes>
  494. static constexpr bool __is_implicitly_constructible()
  495. { return false; }
  496. template<typename... _UTypes>
  497. static constexpr bool __is_explicitly_constructible()
  498. { return false; }
  499. };
  500. /// Primary class template, tuple
  501. template<typename... _Elements>
  502. class tuple : public _Tuple_impl<0, _Elements...>
  503. {
  504. typedef _Tuple_impl<0, _Elements...> _Inherited;
  505. template<bool _Cond>
  506. using _TCC = _TupleConstraints<_Cond, _Elements...>;
  507. // Constraint for non-explicit default constructor
  508. template<bool _Dummy>
  509. using _ImplicitDefaultCtor = __enable_if_t<
  510. _TCC<_Dummy>::__is_implicitly_default_constructible(),
  511. bool>;
  512. // Constraint for explicit default constructor
  513. template<bool _Dummy>
  514. using _ExplicitDefaultCtor = __enable_if_t<
  515. _TCC<_Dummy>::__is_explicitly_default_constructible(),
  516. bool>;
  517. // Constraint for non-explicit constructors
  518. template<bool _Cond, typename... _Args>
  519. using _ImplicitCtor = __enable_if_t<
  520. _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
  521. bool>;
  522. // Constraint for non-explicit constructors
  523. template<bool _Cond, typename... _Args>
  524. using _ExplicitCtor = __enable_if_t<
  525. _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
  526. bool>;
  527. template<typename... _UElements>
  528. static constexpr
  529. __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
  530. __assignable()
  531. { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
  532. // Condition for noexcept-specifier of an assignment operator.
  533. template<typename... _UElements>
  534. static constexpr bool __nothrow_assignable()
  535. {
  536. return
  537. __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
  538. }
  539. // Condition for noexcept-specifier of a constructor.
  540. template<typename... _UElements>
  541. static constexpr bool __nothrow_constructible()
  542. {
  543. return
  544. __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
  545. }
  546. // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
  547. template<typename _Up>
  548. static constexpr bool __valid_args()
  549. {
  550. return sizeof...(_Elements) == 1
  551. && !is_same<tuple, __remove_cvref_t<_Up>>::value;
  552. }
  553. // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
  554. template<typename, typename, typename... _Tail>
  555. static constexpr bool __valid_args()
  556. { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
  557. /* Constraint for constructors with a tuple<UTypes...> parameter ensures
  558. * that the constructor is only viable when it would not interfere with
  559. * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
  560. * Such constructors are only viable if:
  561. * either sizeof...(Types) != 1,
  562. * or (when Types... expands to T and UTypes... expands to U)
  563. * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
  564. * and is_same_v<T, U> are all false.
  565. */
  566. template<typename _Tuple, typename = tuple,
  567. typename = __remove_cvref_t<_Tuple>>
  568. struct _UseOtherCtor
  569. : false_type
  570. { };
  571. // If TUPLE is convertible to the single element in *this,
  572. // then TUPLE should match tuple(UTypes&&...) instead.
  573. template<typename _Tuple, typename _Tp, typename _Up>
  574. struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
  575. : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>
  576. { };
  577. // If TUPLE and *this each have a single element of the same type,
  578. // then TUPLE should match a copy/move constructor instead.
  579. template<typename _Tuple, typename _Tp>
  580. struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
  581. : true_type
  582. { };
  583. // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
  584. // and the single element in Types can be initialized from TUPLE,
  585. // or is the same type as tuple_element_t<0, TUPLE>.
  586. template<typename _Tuple>
  587. static constexpr bool __use_other_ctor()
  588. { return _UseOtherCtor<_Tuple>::value; }
  589. public:
  590. template<typename _Dummy = void,
  591. _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
  592. constexpr
  593. tuple()
  594. noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
  595. : _Inherited() { }
  596. template<typename _Dummy = void,
  597. _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
  598. explicit constexpr
  599. tuple()
  600. noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
  601. : _Inherited() { }
  602. template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
  603. _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
  604. constexpr
  605. tuple(const _Elements&... __elements)
  606. noexcept(__nothrow_constructible<const _Elements&...>())
  607. : _Inherited(__elements...) { }
  608. template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
  609. _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
  610. explicit constexpr
  611. tuple(const _Elements&... __elements)
  612. noexcept(__nothrow_constructible<const _Elements&...>())
  613. : _Inherited(__elements...) { }
  614. template<typename... _UElements,
  615. bool _Valid = __valid_args<_UElements...>(),
  616. _ImplicitCtor<_Valid, _UElements...> = true>
  617. constexpr
  618. tuple(_UElements&&... __elements)
  619. noexcept(__nothrow_constructible<_UElements...>())
  620. : _Inherited(std::forward<_UElements>(__elements)...) { }
  621. template<typename... _UElements,
  622. bool _Valid = __valid_args<_UElements...>(),
  623. _ExplicitCtor<_Valid, _UElements...> = false>
  624. explicit constexpr
  625. tuple(_UElements&&... __elements)
  626. noexcept(__nothrow_constructible<_UElements...>())
  627. : _Inherited(std::forward<_UElements>(__elements)...) { }
  628. constexpr tuple(const tuple&) = default;
  629. constexpr tuple(tuple&&) = default;
  630. template<typename... _UElements,
  631. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  632. && !__use_other_ctor<const tuple<_UElements...>&>(),
  633. _ImplicitCtor<_Valid, const _UElements&...> = true>
  634. constexpr
  635. tuple(const tuple<_UElements...>& __in)
  636. noexcept(__nothrow_constructible<const _UElements&...>())
  637. : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  638. { }
  639. template<typename... _UElements,
  640. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  641. && !__use_other_ctor<const tuple<_UElements...>&>(),
  642. _ExplicitCtor<_Valid, const _UElements&...> = false>
  643. explicit constexpr
  644. tuple(const tuple<_UElements...>& __in)
  645. noexcept(__nothrow_constructible<const _UElements&...>())
  646. : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  647. { }
  648. template<typename... _UElements,
  649. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  650. && !__use_other_ctor<tuple<_UElements...>&&>(),
  651. _ImplicitCtor<_Valid, _UElements...> = true>
  652. constexpr
  653. tuple(tuple<_UElements...>&& __in)
  654. noexcept(__nothrow_constructible<_UElements...>())
  655. : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
  656. template<typename... _UElements,
  657. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  658. && !__use_other_ctor<tuple<_UElements...>&&>(),
  659. _ExplicitCtor<_Valid, _UElements...> = false>
  660. explicit constexpr
  661. tuple(tuple<_UElements...>&& __in)
  662. noexcept(__nothrow_constructible<_UElements...>())
  663. : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
  664. // Allocator-extended constructors.
  665. template<typename _Alloc,
  666. _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
  667. _GLIBCXX20_CONSTEXPR
  668. tuple(allocator_arg_t __tag, const _Alloc& __a)
  669. : _Inherited(__tag, __a) { }
  670. template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
  671. _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
  672. _GLIBCXX20_CONSTEXPR
  673. tuple(allocator_arg_t __tag, const _Alloc& __a,
  674. const _Elements&... __elements)
  675. : _Inherited(__tag, __a, __elements...) { }
  676. template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
  677. _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
  678. _GLIBCXX20_CONSTEXPR
  679. explicit
  680. tuple(allocator_arg_t __tag, const _Alloc& __a,
  681. const _Elements&... __elements)
  682. : _Inherited(__tag, __a, __elements...) { }
  683. template<typename _Alloc, typename... _UElements,
  684. bool _Valid = __valid_args<_UElements...>(),
  685. _ImplicitCtor<_Valid, _UElements...> = true>
  686. _GLIBCXX20_CONSTEXPR
  687. tuple(allocator_arg_t __tag, const _Alloc& __a,
  688. _UElements&&... __elements)
  689. : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
  690. { }
  691. template<typename _Alloc, typename... _UElements,
  692. bool _Valid = __valid_args<_UElements...>(),
  693. _ExplicitCtor<_Valid, _UElements...> = false>
  694. _GLIBCXX20_CONSTEXPR
  695. explicit
  696. tuple(allocator_arg_t __tag, const _Alloc& __a,
  697. _UElements&&... __elements)
  698. : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
  699. { }
  700. template<typename _Alloc>
  701. _GLIBCXX20_CONSTEXPR
  702. tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
  703. : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
  704. template<typename _Alloc>
  705. _GLIBCXX20_CONSTEXPR
  706. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
  707. : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
  708. template<typename _Alloc, typename... _UElements,
  709. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  710. && !__use_other_ctor<const tuple<_UElements...>&>(),
  711. _ImplicitCtor<_Valid, const _UElements&...> = true>
  712. _GLIBCXX20_CONSTEXPR
  713. tuple(allocator_arg_t __tag, const _Alloc& __a,
  714. const tuple<_UElements...>& __in)
  715. : _Inherited(__tag, __a,
  716. static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  717. { }
  718. template<typename _Alloc, typename... _UElements,
  719. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  720. && !__use_other_ctor<const tuple<_UElements...>&>(),
  721. _ExplicitCtor<_Valid, const _UElements&...> = false>
  722. _GLIBCXX20_CONSTEXPR
  723. explicit
  724. tuple(allocator_arg_t __tag, const _Alloc& __a,
  725. const tuple<_UElements...>& __in)
  726. : _Inherited(__tag, __a,
  727. static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  728. { }
  729. template<typename _Alloc, typename... _UElements,
  730. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  731. && !__use_other_ctor<tuple<_UElements...>&&>(),
  732. _ImplicitCtor<_Valid, _UElements...> = true>
  733. _GLIBCXX20_CONSTEXPR
  734. tuple(allocator_arg_t __tag, const _Alloc& __a,
  735. tuple<_UElements...>&& __in)
  736. : _Inherited(__tag, __a,
  737. static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
  738. { }
  739. template<typename _Alloc, typename... _UElements,
  740. bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
  741. && !__use_other_ctor<tuple<_UElements...>&&>(),
  742. _ExplicitCtor<_Valid, _UElements...> = false>
  743. _GLIBCXX20_CONSTEXPR
  744. explicit
  745. tuple(allocator_arg_t __tag, const _Alloc& __a,
  746. tuple<_UElements...>&& __in)
  747. : _Inherited(__tag, __a,
  748. static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
  749. { }
  750. // tuple assignment
  751. _GLIBCXX20_CONSTEXPR
  752. tuple&
  753. operator=(__conditional_t<__assignable<const _Elements&...>(),
  754. const tuple&,
  755. const __nonesuch&> __in)
  756. noexcept(__nothrow_assignable<const _Elements&...>())
  757. {
  758. this->_M_assign(__in);
  759. return *this;
  760. }
  761. _GLIBCXX20_CONSTEXPR
  762. tuple&
  763. operator=(__conditional_t<__assignable<_Elements...>(),
  764. tuple&&,
  765. __nonesuch&&> __in)
  766. noexcept(__nothrow_assignable<_Elements...>())
  767. {
  768. this->_M_assign(std::move(__in));
  769. return *this;
  770. }
  771. template<typename... _UElements>
  772. _GLIBCXX20_CONSTEXPR
  773. __enable_if_t<__assignable<const _UElements&...>(), tuple&>
  774. operator=(const tuple<_UElements...>& __in)
  775. noexcept(__nothrow_assignable<const _UElements&...>())
  776. {
  777. this->_M_assign(__in);
  778. return *this;
  779. }
  780. template<typename... _UElements>
  781. _GLIBCXX20_CONSTEXPR
  782. __enable_if_t<__assignable<_UElements...>(), tuple&>
  783. operator=(tuple<_UElements...>&& __in)
  784. noexcept(__nothrow_assignable<_UElements...>())
  785. {
  786. this->_M_assign(std::move(__in));
  787. return *this;
  788. }
  789. // tuple swap
  790. _GLIBCXX20_CONSTEXPR
  791. void
  792. swap(tuple& __in)
  793. noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
  794. { _Inherited::_M_swap(__in); }
  795. };
  796. #if __cpp_deduction_guides >= 201606
  797. template<typename... _UTypes>
  798. tuple(_UTypes...) -> tuple<_UTypes...>;
  799. template<typename _T1, typename _T2>
  800. tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
  801. template<typename _Alloc, typename... _UTypes>
  802. tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
  803. template<typename _Alloc, typename _T1, typename _T2>
  804. tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
  805. template<typename _Alloc, typename... _UTypes>
  806. tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
  807. #endif
  808. // Explicit specialization, zero-element tuple.
  809. template<>
  810. class tuple<>
  811. {
  812. public:
  813. _GLIBCXX20_CONSTEXPR
  814. void swap(tuple&) noexcept { /* no-op */ }
  815. // We need the default since we're going to define no-op
  816. // allocator constructors.
  817. tuple() = default;
  818. // No-op allocator constructors.
  819. template<typename _Alloc>
  820. _GLIBCXX20_CONSTEXPR
  821. tuple(allocator_arg_t, const _Alloc&) noexcept { }
  822. template<typename _Alloc>
  823. _GLIBCXX20_CONSTEXPR
  824. tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
  825. };
  826. /// Partial specialization, 2-element tuple.
  827. /// Includes construction and assignment from a pair.
  828. template<typename _T1, typename _T2>
  829. class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
  830. {
  831. typedef _Tuple_impl<0, _T1, _T2> _Inherited;
  832. // Constraint for non-explicit default constructor
  833. template<bool _Dummy, typename _U1, typename _U2>
  834. using _ImplicitDefaultCtor = __enable_if_t<
  835. _TupleConstraints<_Dummy, _U1, _U2>::
  836. __is_implicitly_default_constructible(),
  837. bool>;
  838. // Constraint for explicit default constructor
  839. template<bool _Dummy, typename _U1, typename _U2>
  840. using _ExplicitDefaultCtor = __enable_if_t<
  841. _TupleConstraints<_Dummy, _U1, _U2>::
  842. __is_explicitly_default_constructible(),
  843. bool>;
  844. template<bool _Dummy>
  845. using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
  846. // Constraint for non-explicit constructors
  847. template<bool _Cond, typename _U1, typename _U2>
  848. using _ImplicitCtor = __enable_if_t<
  849. _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
  850. bool>;
  851. // Constraint for non-explicit constructors
  852. template<bool _Cond, typename _U1, typename _U2>
  853. using _ExplicitCtor = __enable_if_t<
  854. _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
  855. bool>;
  856. template<typename _U1, typename _U2>
  857. static constexpr bool __assignable()
  858. {
  859. return __and_<is_assignable<_T1&, _U1>,
  860. is_assignable<_T2&, _U2>>::value;
  861. }
  862. template<typename _U1, typename _U2>
  863. static constexpr bool __nothrow_assignable()
  864. {
  865. return __and_<is_nothrow_assignable<_T1&, _U1>,
  866. is_nothrow_assignable<_T2&, _U2>>::value;
  867. }
  868. template<typename _U1, typename _U2>
  869. static constexpr bool __nothrow_constructible()
  870. {
  871. return __and_<is_nothrow_constructible<_T1, _U1>,
  872. is_nothrow_constructible<_T2, _U2>>::value;
  873. }
  874. static constexpr bool __nothrow_default_constructible()
  875. {
  876. return __and_<is_nothrow_default_constructible<_T1>,
  877. is_nothrow_default_constructible<_T2>>::value;
  878. }
  879. template<typename _U1>
  880. static constexpr bool __is_alloc_arg()
  881. { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
  882. public:
  883. template<bool _Dummy = true,
  884. _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
  885. constexpr
  886. tuple()
  887. noexcept(__nothrow_default_constructible())
  888. : _Inherited() { }
  889. template<bool _Dummy = true,
  890. _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
  891. explicit constexpr
  892. tuple()
  893. noexcept(__nothrow_default_constructible())
  894. : _Inherited() { }
  895. template<bool _Dummy = true,
  896. _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
  897. constexpr
  898. tuple(const _T1& __a1, const _T2& __a2)
  899. noexcept(__nothrow_constructible<const _T1&, const _T2&>())
  900. : _Inherited(__a1, __a2) { }
  901. template<bool _Dummy = true,
  902. _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
  903. explicit constexpr
  904. tuple(const _T1& __a1, const _T2& __a2)
  905. noexcept(__nothrow_constructible<const _T1&, const _T2&>())
  906. : _Inherited(__a1, __a2) { }
  907. template<typename _U1, typename _U2,
  908. _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
  909. constexpr
  910. tuple(_U1&& __a1, _U2&& __a2)
  911. noexcept(__nothrow_constructible<_U1, _U2>())
  912. : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
  913. template<typename _U1, typename _U2,
  914. _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
  915. explicit constexpr
  916. tuple(_U1&& __a1, _U2&& __a2)
  917. noexcept(__nothrow_constructible<_U1, _U2>())
  918. : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
  919. constexpr tuple(const tuple&) = default;
  920. constexpr tuple(tuple&&) = default;
  921. template<typename _U1, typename _U2,
  922. _ImplicitCtor<true, const _U1&, const _U2&> = true>
  923. constexpr
  924. tuple(const tuple<_U1, _U2>& __in)
  925. noexcept(__nothrow_constructible<const _U1&, const _U2&>())
  926. : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
  927. template<typename _U1, typename _U2,
  928. _ExplicitCtor<true, const _U1&, const _U2&> = false>
  929. explicit constexpr
  930. tuple(const tuple<_U1, _U2>& __in)
  931. noexcept(__nothrow_constructible<const _U1&, const _U2&>())
  932. : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
  933. template<typename _U1, typename _U2,
  934. _ImplicitCtor<true, _U1, _U2> = true>
  935. constexpr
  936. tuple(tuple<_U1, _U2>&& __in)
  937. noexcept(__nothrow_constructible<_U1, _U2>())
  938. : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
  939. template<typename _U1, typename _U2,
  940. _ExplicitCtor<true, _U1, _U2> = false>
  941. explicit constexpr
  942. tuple(tuple<_U1, _U2>&& __in)
  943. noexcept(__nothrow_constructible<_U1, _U2>())
  944. : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
  945. template<typename _U1, typename _U2,
  946. _ImplicitCtor<true, const _U1&, const _U2&> = true>
  947. constexpr
  948. tuple(const pair<_U1, _U2>& __in)
  949. noexcept(__nothrow_constructible<const _U1&, const _U2&>())
  950. : _Inherited(__in.first, __in.second) { }
  951. template<typename _U1, typename _U2,
  952. _ExplicitCtor<true, const _U1&, const _U2&> = false>
  953. explicit constexpr
  954. tuple(const pair<_U1, _U2>& __in)
  955. noexcept(__nothrow_constructible<const _U1&, const _U2&>())
  956. : _Inherited(__in.first, __in.second) { }
  957. template<typename _U1, typename _U2,
  958. _ImplicitCtor<true, _U1, _U2> = true>
  959. constexpr
  960. tuple(pair<_U1, _U2>&& __in)
  961. noexcept(__nothrow_constructible<_U1, _U2>())
  962. : _Inherited(std::forward<_U1>(__in.first),
  963. std::forward<_U2>(__in.second)) { }
  964. template<typename _U1, typename _U2,
  965. _ExplicitCtor<true, _U1, _U2> = false>
  966. explicit constexpr
  967. tuple(pair<_U1, _U2>&& __in)
  968. noexcept(__nothrow_constructible<_U1, _U2>())
  969. : _Inherited(std::forward<_U1>(__in.first),
  970. std::forward<_U2>(__in.second)) { }
  971. // Allocator-extended constructors.
  972. template<typename _Alloc,
  973. _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
  974. _GLIBCXX20_CONSTEXPR
  975. tuple(allocator_arg_t __tag, const _Alloc& __a)
  976. : _Inherited(__tag, __a) { }
  977. template<typename _Alloc, bool _Dummy = true,
  978. _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
  979. _GLIBCXX20_CONSTEXPR
  980. tuple(allocator_arg_t __tag, const _Alloc& __a,
  981. const _T1& __a1, const _T2& __a2)
  982. : _Inherited(__tag, __a, __a1, __a2) { }
  983. template<typename _Alloc, bool _Dummy = true,
  984. _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
  985. explicit
  986. _GLIBCXX20_CONSTEXPR
  987. tuple(allocator_arg_t __tag, const _Alloc& __a,
  988. const _T1& __a1, const _T2& __a2)
  989. : _Inherited(__tag, __a, __a1, __a2) { }
  990. template<typename _Alloc, typename _U1, typename _U2,
  991. _ImplicitCtor<true, _U1, _U2> = true>
  992. _GLIBCXX20_CONSTEXPR
  993. tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
  994. : _Inherited(__tag, __a, std::forward<_U1>(__a1),
  995. std::forward<_U2>(__a2)) { }
  996. template<typename _Alloc, typename _U1, typename _U2,
  997. _ExplicitCtor<true, _U1, _U2> = false>
  998. explicit
  999. _GLIBCXX20_CONSTEXPR
  1000. tuple(allocator_arg_t __tag, const _Alloc& __a,
  1001. _U1&& __a1, _U2&& __a2)
  1002. : _Inherited(__tag, __a, std::forward<_U1>(__a1),
  1003. std::forward<_U2>(__a2)) { }
  1004. template<typename _Alloc>
  1005. _GLIBCXX20_CONSTEXPR
  1006. tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
  1007. : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
  1008. template<typename _Alloc>
  1009. _GLIBCXX20_CONSTEXPR
  1010. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
  1011. : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
  1012. template<typename _Alloc, typename _U1, typename _U2,
  1013. _ImplicitCtor<true, const _U1&, const _U2&> = true>
  1014. _GLIBCXX20_CONSTEXPR
  1015. tuple(allocator_arg_t __tag, const _Alloc& __a,
  1016. const tuple<_U1, _U2>& __in)
  1017. : _Inherited(__tag, __a,
  1018. static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
  1019. { }
  1020. template<typename _Alloc, typename _U1, typename _U2,
  1021. _ExplicitCtor<true, const _U1&, const _U2&> = false>
  1022. explicit
  1023. _GLIBCXX20_CONSTEXPR
  1024. tuple(allocator_arg_t __tag, const _Alloc& __a,
  1025. const tuple<_U1, _U2>& __in)
  1026. : _Inherited(__tag, __a,
  1027. static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
  1028. { }
  1029. template<typename _Alloc, typename _U1, typename _U2,
  1030. _ImplicitCtor<true, _U1, _U2> = true>
  1031. _GLIBCXX20_CONSTEXPR
  1032. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
  1033. : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
  1034. { }
  1035. template<typename _Alloc, typename _U1, typename _U2,
  1036. _ExplicitCtor<true, _U1, _U2> = false>
  1037. explicit
  1038. _GLIBCXX20_CONSTEXPR
  1039. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
  1040. : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
  1041. { }
  1042. template<typename _Alloc, typename _U1, typename _U2,
  1043. _ImplicitCtor<true, const _U1&, const _U2&> = true>
  1044. _GLIBCXX20_CONSTEXPR
  1045. tuple(allocator_arg_t __tag, const _Alloc& __a,
  1046. const pair<_U1, _U2>& __in)
  1047. : _Inherited(__tag, __a, __in.first, __in.second) { }
  1048. template<typename _Alloc, typename _U1, typename _U2,
  1049. _ExplicitCtor<true, const _U1&, const _U2&> = false>
  1050. explicit
  1051. _GLIBCXX20_CONSTEXPR
  1052. tuple(allocator_arg_t __tag, const _Alloc& __a,
  1053. const pair<_U1, _U2>& __in)
  1054. : _Inherited(__tag, __a, __in.first, __in.second) { }
  1055. template<typename _Alloc, typename _U1, typename _U2,
  1056. _ImplicitCtor<true, _U1, _U2> = true>
  1057. _GLIBCXX20_CONSTEXPR
  1058. tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
  1059. : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
  1060. std::forward<_U2>(__in.second)) { }
  1061. template<typename _Alloc, typename _U1, typename _U2,
  1062. _ExplicitCtor<true, _U1, _U2> = false>
  1063. explicit
  1064. _GLIBCXX20_CONSTEXPR
  1065. tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
  1066. : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
  1067. std::forward<_U2>(__in.second)) { }
  1068. // Tuple assignment.
  1069. _GLIBCXX20_CONSTEXPR
  1070. tuple&
  1071. operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
  1072. const tuple&,
  1073. const __nonesuch&> __in)
  1074. noexcept(__nothrow_assignable<const _T1&, const _T2&>())
  1075. {
  1076. this->_M_assign(__in);
  1077. return *this;
  1078. }
  1079. _GLIBCXX20_CONSTEXPR
  1080. tuple&
  1081. operator=(__conditional_t<__assignable<_T1, _T2>(),
  1082. tuple&&,
  1083. __nonesuch&&> __in)
  1084. noexcept(__nothrow_assignable<_T1, _T2>())
  1085. {
  1086. this->_M_assign(std::move(__in));
  1087. return *this;
  1088. }
  1089. template<typename _U1, typename _U2>
  1090. _GLIBCXX20_CONSTEXPR
  1091. __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
  1092. operator=(const tuple<_U1, _U2>& __in)
  1093. noexcept(__nothrow_assignable<const _U1&, const _U2&>())
  1094. {
  1095. this->_M_assign(__in);
  1096. return *this;
  1097. }
  1098. template<typename _U1, typename _U2>
  1099. _GLIBCXX20_CONSTEXPR
  1100. __enable_if_t<__assignable<_U1, _U2>(), tuple&>
  1101. operator=(tuple<_U1, _U2>&& __in)
  1102. noexcept(__nothrow_assignable<_U1, _U2>())
  1103. {
  1104. this->_M_assign(std::move(__in));
  1105. return *this;
  1106. }
  1107. template<typename _U1, typename _U2>
  1108. _GLIBCXX20_CONSTEXPR
  1109. __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
  1110. operator=(const pair<_U1, _U2>& __in)
  1111. noexcept(__nothrow_assignable<const _U1&, const _U2&>())
  1112. {
  1113. this->_M_head(*this) = __in.first;
  1114. this->_M_tail(*this)._M_head(*this) = __in.second;
  1115. return *this;
  1116. }
  1117. template<typename _U1, typename _U2>
  1118. _GLIBCXX20_CONSTEXPR
  1119. __enable_if_t<__assignable<_U1, _U2>(), tuple&>
  1120. operator=(pair<_U1, _U2>&& __in)
  1121. noexcept(__nothrow_assignable<_U1, _U2>())
  1122. {
  1123. this->_M_head(*this) = std::forward<_U1>(__in.first);
  1124. this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
  1125. return *this;
  1126. }
  1127. _GLIBCXX20_CONSTEXPR
  1128. void
  1129. swap(tuple& __in)
  1130. noexcept(__and_<__is_nothrow_swappable<_T1>,
  1131. __is_nothrow_swappable<_T2>>::value)
  1132. { _Inherited::_M_swap(__in); }
  1133. };
  1134. /// class tuple_size
  1135. template<typename... _Elements>
  1136. struct tuple_size<tuple<_Elements...>>
  1137. : public integral_constant<size_t, sizeof...(_Elements)> { };
  1138. #if __cplusplus >= 201703L
  1139. template<typename... _Types>
  1140. inline constexpr size_t tuple_size_v<tuple<_Types...>>
  1141. = sizeof...(_Types);
  1142. template<typename... _Types>
  1143. inline constexpr size_t tuple_size_v<const tuple<_Types...>>
  1144. = sizeof...(_Types);
  1145. #endif
  1146. /// Trait to get the Ith element type from a tuple.
  1147. template<size_t __i, typename... _Types>
  1148. struct tuple_element<__i, tuple<_Types...>>
  1149. {
  1150. static_assert(__i < sizeof...(_Types), "tuple index must be in range");
  1151. using type = typename _Nth_type<__i, _Types...>::type;
  1152. };
  1153. template<size_t __i, typename _Head, typename... _Tail>
  1154. constexpr _Head&
  1155. __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1156. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1157. template<size_t __i, typename _Head, typename... _Tail>
  1158. constexpr const _Head&
  1159. __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1160. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1161. // Deleted overload to improve diagnostics for invalid indices
  1162. template<size_t __i, typename... _Types>
  1163. __enable_if_t<(__i >= sizeof...(_Types))>
  1164. __get_helper(const tuple<_Types...>&) = delete;
  1165. /// Return a reference to the ith element of a tuple.
  1166. template<size_t __i, typename... _Elements>
  1167. constexpr __tuple_element_t<__i, tuple<_Elements...>>&
  1168. get(tuple<_Elements...>& __t) noexcept
  1169. { return std::__get_helper<__i>(__t); }
  1170. /// Return a const reference to the ith element of a const tuple.
  1171. template<size_t __i, typename... _Elements>
  1172. constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
  1173. get(const tuple<_Elements...>& __t) noexcept
  1174. { return std::__get_helper<__i>(__t); }
  1175. /// Return an rvalue reference to the ith element of a tuple rvalue.
  1176. template<size_t __i, typename... _Elements>
  1177. constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
  1178. get(tuple<_Elements...>&& __t) noexcept
  1179. {
  1180. typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
  1181. return std::forward<__element_type>(std::__get_helper<__i>(__t));
  1182. }
  1183. /// Return a const rvalue reference to the ith element of a const tuple rvalue.
  1184. template<size_t __i, typename... _Elements>
  1185. constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
  1186. get(const tuple<_Elements...>&& __t) noexcept
  1187. {
  1188. typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
  1189. return std::forward<const __element_type>(std::__get_helper<__i>(__t));
  1190. }
  1191. /// @cond undocumented
  1192. // Deleted overload chosen for invalid indices.
  1193. template<size_t __i, typename... _Elements>
  1194. constexpr __enable_if_t<(__i >= sizeof...(_Elements))>
  1195. get(const tuple<_Elements...>&) = delete;
  1196. /// @endcond
  1197. #if __cplusplus >= 201402L
  1198. #define __cpp_lib_tuples_by_type 201304L
  1199. /// Return a reference to the unique element of type _Tp of a tuple.
  1200. template <typename _Tp, typename... _Types>
  1201. constexpr _Tp&
  1202. get(tuple<_Types...>& __t) noexcept
  1203. {
  1204. constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
  1205. static_assert(__idx < sizeof...(_Types),
  1206. "the type T in std::get<T> must occur exactly once in the tuple");
  1207. return std::__get_helper<__idx>(__t);
  1208. }
  1209. /// Return a reference to the unique element of type _Tp of a tuple rvalue.
  1210. template <typename _Tp, typename... _Types>
  1211. constexpr _Tp&&
  1212. get(tuple<_Types...>&& __t) noexcept
  1213. {
  1214. constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
  1215. static_assert(__idx < sizeof...(_Types),
  1216. "the type T in std::get<T> must occur exactly once in the tuple");
  1217. return std::forward<_Tp>(std::__get_helper<__idx>(__t));
  1218. }
  1219. /// Return a const reference to the unique element of type _Tp of a tuple.
  1220. template <typename _Tp, typename... _Types>
  1221. constexpr const _Tp&
  1222. get(const tuple<_Types...>& __t) noexcept
  1223. {
  1224. constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
  1225. static_assert(__idx < sizeof...(_Types),
  1226. "the type T in std::get<T> must occur exactly once in the tuple");
  1227. return std::__get_helper<__idx>(__t);
  1228. }
  1229. /// Return a const reference to the unique element of type _Tp of
  1230. /// a const tuple rvalue.
  1231. template <typename _Tp, typename... _Types>
  1232. constexpr const _Tp&&
  1233. get(const tuple<_Types...>&& __t) noexcept
  1234. {
  1235. constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
  1236. static_assert(__idx < sizeof...(_Types),
  1237. "the type T in std::get<T> must occur exactly once in the tuple");
  1238. return std::forward<const _Tp>(std::__get_helper<__idx>(__t));
  1239. }
  1240. #endif
  1241. // This class performs the comparison operations on tuples
  1242. template<typename _Tp, typename _Up, size_t __i, size_t __size>
  1243. struct __tuple_compare
  1244. {
  1245. static constexpr bool
  1246. __eq(const _Tp& __t, const _Up& __u)
  1247. {
  1248. return bool(std::get<__i>(__t) == std::get<__i>(__u))
  1249. && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
  1250. }
  1251. static constexpr bool
  1252. __less(const _Tp& __t, const _Up& __u)
  1253. {
  1254. return bool(std::get<__i>(__t) < std::get<__i>(__u))
  1255. || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
  1256. && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
  1257. }
  1258. };
  1259. template<typename _Tp, typename _Up, size_t __size>
  1260. struct __tuple_compare<_Tp, _Up, __size, __size>
  1261. {
  1262. static constexpr bool
  1263. __eq(const _Tp&, const _Up&) { return true; }
  1264. static constexpr bool
  1265. __less(const _Tp&, const _Up&) { return false; }
  1266. };
  1267. template<typename... _TElements, typename... _UElements>
  1268. constexpr bool
  1269. operator==(const tuple<_TElements...>& __t,
  1270. const tuple<_UElements...>& __u)
  1271. {
  1272. static_assert(sizeof...(_TElements) == sizeof...(_UElements),
  1273. "tuple objects can only be compared if they have equal sizes.");
  1274. using __compare = __tuple_compare<tuple<_TElements...>,
  1275. tuple<_UElements...>,
  1276. 0, sizeof...(_TElements)>;
  1277. return __compare::__eq(__t, __u);
  1278. }
  1279. #if __cpp_lib_three_way_comparison
  1280. template<typename _Cat, typename _Tp, typename _Up>
  1281. constexpr _Cat
  1282. __tuple_cmp(const _Tp&, const _Up&, index_sequence<>)
  1283. { return _Cat::equivalent; }
  1284. template<typename _Cat, typename _Tp, typename _Up,
  1285. size_t _Idx0, size_t... _Idxs>
  1286. constexpr _Cat
  1287. __tuple_cmp(const _Tp& __t, const _Up& __u,
  1288. index_sequence<_Idx0, _Idxs...>)
  1289. {
  1290. auto __c
  1291. = __detail::__synth3way(std::get<_Idx0>(__t), std::get<_Idx0>(__u));
  1292. if (__c != 0)
  1293. return __c;
  1294. return std::__tuple_cmp<_Cat>(__t, __u, index_sequence<_Idxs...>());
  1295. }
  1296. template<typename... _Tps, typename... _Ups>
  1297. constexpr
  1298. common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>
  1299. operator<=>(const tuple<_Tps...>& __t, const tuple<_Ups...>& __u)
  1300. {
  1301. using _Cat
  1302. = common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>;
  1303. return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Tps...>());
  1304. }
  1305. #else
  1306. template<typename... _TElements, typename... _UElements>
  1307. constexpr bool
  1308. operator<(const tuple<_TElements...>& __t,
  1309. const tuple<_UElements...>& __u)
  1310. {
  1311. static_assert(sizeof...(_TElements) == sizeof...(_UElements),
  1312. "tuple objects can only be compared if they have equal sizes.");
  1313. using __compare = __tuple_compare<tuple<_TElements...>,
  1314. tuple<_UElements...>,
  1315. 0, sizeof...(_TElements)>;
  1316. return __compare::__less(__t, __u);
  1317. }
  1318. template<typename... _TElements, typename... _UElements>
  1319. constexpr bool
  1320. operator!=(const tuple<_TElements...>& __t,
  1321. const tuple<_UElements...>& __u)
  1322. { return !(__t == __u); }
  1323. template<typename... _TElements, typename... _UElements>
  1324. constexpr bool
  1325. operator>(const tuple<_TElements...>& __t,
  1326. const tuple<_UElements...>& __u)
  1327. { return __u < __t; }
  1328. template<typename... _TElements, typename... _UElements>
  1329. constexpr bool
  1330. operator<=(const tuple<_TElements...>& __t,
  1331. const tuple<_UElements...>& __u)
  1332. { return !(__u < __t); }
  1333. template<typename... _TElements, typename... _UElements>
  1334. constexpr bool
  1335. operator>=(const tuple<_TElements...>& __t,
  1336. const tuple<_UElements...>& __u)
  1337. { return !(__t < __u); }
  1338. #endif // three_way_comparison
  1339. // NB: DR 705.
  1340. template<typename... _Elements>
  1341. constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
  1342. make_tuple(_Elements&&... __args)
  1343. {
  1344. typedef tuple<typename __decay_and_strip<_Elements>::__type...>
  1345. __result_type;
  1346. return __result_type(std::forward<_Elements>(__args)...);
  1347. }
  1348. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1349. // 2275. Why is forward_as_tuple not constexpr?
  1350. /// std::forward_as_tuple
  1351. template<typename... _Elements>
  1352. constexpr tuple<_Elements&&...>
  1353. forward_as_tuple(_Elements&&... __args) noexcept
  1354. { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
  1355. // Declarations of std::array and its std::get overloads, so that
  1356. // std::tuple_cat can use them if <tuple> is included before <array>.
  1357. template<typename _Tp, size_t _Nm> struct array;
  1358. template<size_t _Int, typename _Tp, size_t _Nm>
  1359. constexpr _Tp&
  1360. get(array<_Tp, _Nm>&) noexcept;
  1361. template<size_t _Int, typename _Tp, size_t _Nm>
  1362. constexpr _Tp&&
  1363. get(array<_Tp, _Nm>&&) noexcept;
  1364. template<size_t _Int, typename _Tp, size_t _Nm>
  1365. constexpr const _Tp&
  1366. get(const array<_Tp, _Nm>&) noexcept;
  1367. template<size_t _Int, typename _Tp, size_t _Nm>
  1368. constexpr const _Tp&&
  1369. get(const array<_Tp, _Nm>&&) noexcept;
  1370. template<size_t, typename, typename, size_t>
  1371. struct __make_tuple_impl;
  1372. template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
  1373. struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
  1374. : __make_tuple_impl<_Idx + 1,
  1375. tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
  1376. _Tuple, _Nm>
  1377. { };
  1378. template<size_t _Nm, typename _Tuple, typename... _Tp>
  1379. struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
  1380. {
  1381. typedef tuple<_Tp...> __type;
  1382. };
  1383. template<typename _Tuple>
  1384. struct __do_make_tuple
  1385. : __make_tuple_impl<0, tuple<>, _Tuple, tuple_size<_Tuple>::value>
  1386. { };
  1387. // Returns the std::tuple equivalent of a tuple-like type.
  1388. template<typename _Tuple>
  1389. struct __make_tuple
  1390. : public __do_make_tuple<__remove_cvref_t<_Tuple>>
  1391. { };
  1392. // Combines several std::tuple's into a single one.
  1393. template<typename...>
  1394. struct __combine_tuples;
  1395. template<>
  1396. struct __combine_tuples<>
  1397. {
  1398. typedef tuple<> __type;
  1399. };
  1400. template<typename... _Ts>
  1401. struct __combine_tuples<tuple<_Ts...>>
  1402. {
  1403. typedef tuple<_Ts...> __type;
  1404. };
  1405. template<typename... _T1s, typename... _T2s, typename... _Rem>
  1406. struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
  1407. {
  1408. typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
  1409. _Rem...>::__type __type;
  1410. };
  1411. // Computes the result type of tuple_cat given a set of tuple-like types.
  1412. template<typename... _Tpls>
  1413. struct __tuple_cat_result
  1414. {
  1415. typedef typename __combine_tuples
  1416. <typename __make_tuple<_Tpls>::__type...>::__type __type;
  1417. };
  1418. // Helper to determine the index set for the first tuple-like
  1419. // type of a given set.
  1420. template<typename...>
  1421. struct __make_1st_indices;
  1422. template<>
  1423. struct __make_1st_indices<>
  1424. {
  1425. typedef _Index_tuple<> __type;
  1426. };
  1427. template<typename _Tp, typename... _Tpls>
  1428. struct __make_1st_indices<_Tp, _Tpls...>
  1429. {
  1430. typedef typename _Build_index_tuple<tuple_size<
  1431. typename remove_reference<_Tp>::type>::value>::__type __type;
  1432. };
  1433. // Performs the actual concatenation by step-wise expanding tuple-like
  1434. // objects into the elements, which are finally forwarded into the
  1435. // result tuple.
  1436. template<typename _Ret, typename _Indices, typename... _Tpls>
  1437. struct __tuple_concater;
  1438. template<typename _Ret, size_t... _Is, typename _Tp, typename... _Tpls>
  1439. struct __tuple_concater<_Ret, _Index_tuple<_Is...>, _Tp, _Tpls...>
  1440. {
  1441. template<typename... _Us>
  1442. static constexpr _Ret
  1443. _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
  1444. {
  1445. typedef typename __make_1st_indices<_Tpls...>::__type __idx;
  1446. typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
  1447. return __next::_S_do(std::forward<_Tpls>(__tps)...,
  1448. std::forward<_Us>(__us)...,
  1449. std::get<_Is>(std::forward<_Tp>(__tp))...);
  1450. }
  1451. };
  1452. template<typename _Ret>
  1453. struct __tuple_concater<_Ret, _Index_tuple<>>
  1454. {
  1455. template<typename... _Us>
  1456. static constexpr _Ret
  1457. _S_do(_Us&&... __us)
  1458. {
  1459. return _Ret(std::forward<_Us>(__us)...);
  1460. }
  1461. };
  1462. template<typename... _Tps>
  1463. struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
  1464. { };
  1465. /// tuple_cat
  1466. template<typename... _Tpls, typename = typename
  1467. enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
  1468. constexpr auto
  1469. tuple_cat(_Tpls&&... __tpls)
  1470. -> typename __tuple_cat_result<_Tpls...>::__type
  1471. {
  1472. typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
  1473. typedef typename __make_1st_indices<_Tpls...>::__type __idx;
  1474. typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
  1475. return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
  1476. }
  1477. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1478. // 2301. Why is tie not constexpr?
  1479. /// tie
  1480. template<typename... _Elements>
  1481. constexpr tuple<_Elements&...>
  1482. tie(_Elements&... __args) noexcept
  1483. { return tuple<_Elements&...>(__args...); }
  1484. /// swap
  1485. template<typename... _Elements>
  1486. _GLIBCXX20_CONSTEXPR
  1487. inline
  1488. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  1489. // Constrained free swap overload, see p0185r1
  1490. typename enable_if<__and_<__is_swappable<_Elements>...>::value
  1491. >::type
  1492. #else
  1493. void
  1494. #endif
  1495. swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
  1496. noexcept(noexcept(__x.swap(__y)))
  1497. { __x.swap(__y); }
  1498. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  1499. template<typename... _Elements>
  1500. _GLIBCXX20_CONSTEXPR
  1501. typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
  1502. swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
  1503. #endif
  1504. // A class (and instance) which can be used in 'tie' when an element
  1505. // of a tuple is not required.
  1506. // _GLIBCXX14_CONSTEXPR
  1507. // 2933. PR for LWG 2773 could be clearer
  1508. struct _Swallow_assign
  1509. {
  1510. template<class _Tp>
  1511. _GLIBCXX14_CONSTEXPR const _Swallow_assign&
  1512. operator=(const _Tp&) const
  1513. { return *this; }
  1514. };
  1515. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1516. // 2773. Making std::ignore constexpr
  1517. _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
  1518. /// Partial specialization for tuples
  1519. template<typename... _Types, typename _Alloc>
  1520. struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
  1521. // See stl_pair.h...
  1522. /** "piecewise construction" using a tuple of arguments for each member.
  1523. *
  1524. * @param __first Arguments for the first member of the pair.
  1525. * @param __second Arguments for the second member of the pair.
  1526. *
  1527. * The elements of each tuple will be used as the constructor arguments
  1528. * for the data members of the pair.
  1529. */
  1530. template<class _T1, class _T2>
  1531. template<typename... _Args1, typename... _Args2>
  1532. _GLIBCXX20_CONSTEXPR
  1533. inline
  1534. pair<_T1, _T2>::
  1535. pair(piecewise_construct_t,
  1536. tuple<_Args1...> __first, tuple<_Args2...> __second)
  1537. : pair(__first, __second,
  1538. typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
  1539. typename _Build_index_tuple<sizeof...(_Args2)>::__type())
  1540. { }
  1541. template<class _T1, class _T2>
  1542. template<typename... _Args1, size_t... _Indexes1,
  1543. typename... _Args2, size_t... _Indexes2>
  1544. _GLIBCXX20_CONSTEXPR inline
  1545. pair<_T1, _T2>::
  1546. pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
  1547. _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
  1548. : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
  1549. second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
  1550. { }
  1551. #if __cplusplus >= 201703L
  1552. // Unpack a std::tuple into a type trait and use its value.
  1553. // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
  1554. // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
  1555. // Otherwise the result is false (because we don't know if std::get throws).
  1556. template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
  1557. inline constexpr bool __unpack_std_tuple = false;
  1558. template<template<typename...> class _Trait, typename _Tp, typename... _Up>
  1559. inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
  1560. = _Trait<_Tp, _Up...>::value;
  1561. template<template<typename...> class _Trait, typename _Tp, typename... _Up>
  1562. inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
  1563. = _Trait<_Tp, _Up&...>::value;
  1564. template<template<typename...> class _Trait, typename _Tp, typename... _Up>
  1565. inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>>
  1566. = _Trait<_Tp, const _Up...>::value;
  1567. template<template<typename...> class _Trait, typename _Tp, typename... _Up>
  1568. inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>
  1569. = _Trait<_Tp, const _Up&...>::value;
  1570. # define __cpp_lib_apply 201603L
  1571. template <typename _Fn, typename _Tuple, size_t... _Idx>
  1572. constexpr decltype(auto)
  1573. __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
  1574. {
  1575. return std::__invoke(std::forward<_Fn>(__f),
  1576. std::get<_Idx>(std::forward<_Tuple>(__t))...);
  1577. }
  1578. template <typename _Fn, typename _Tuple>
  1579. constexpr decltype(auto)
  1580. apply(_Fn&& __f, _Tuple&& __t)
  1581. noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
  1582. {
  1583. using _Indices
  1584. = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
  1585. return std::__apply_impl(std::forward<_Fn>(__f),
  1586. std::forward<_Tuple>(__t),
  1587. _Indices{});
  1588. }
  1589. #define __cpp_lib_make_from_tuple 201606L
  1590. template <typename _Tp, typename _Tuple, size_t... _Idx>
  1591. constexpr _Tp
  1592. __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
  1593. { return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...); }
  1594. template <typename _Tp, typename _Tuple>
  1595. constexpr _Tp
  1596. make_from_tuple(_Tuple&& __t)
  1597. noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
  1598. {
  1599. return __make_from_tuple_impl<_Tp>(
  1600. std::forward<_Tuple>(__t),
  1601. make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{});
  1602. }
  1603. #endif // C++17
  1604. /// @}
  1605. _GLIBCXX_END_NAMESPACE_VERSION
  1606. } // namespace std
  1607. #endif // C++11
  1608. #endif // _GLIBCXX_TUPLE