tuple 60 KB

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