tuple 59 KB

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