tuple 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. // <tuple> -*- C++ -*-
  2. // Copyright (C) 2007-2016 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. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @addtogroup utilities
  37. * @{
  38. */
  39. template<std::size_t _Idx, typename _Head, bool _IsEmptyNotFinal>
  40. struct _Head_base;
  41. template<std::size_t _Idx, typename _Head>
  42. struct _Head_base<_Idx, _Head, true>
  43. : public _Head
  44. {
  45. constexpr _Head_base()
  46. : _Head() { }
  47. constexpr _Head_base(const _Head& __h)
  48. : _Head(__h) { }
  49. constexpr _Head_base(const _Head_base&) = default;
  50. constexpr _Head_base(_Head_base&&) = default;
  51. template<typename _UHead>
  52. constexpr _Head_base(_UHead&& __h)
  53. : _Head(std::forward<_UHead>(__h)) { }
  54. _Head_base(allocator_arg_t, __uses_alloc0)
  55. : _Head() { }
  56. template<typename _Alloc>
  57. _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
  58. : _Head(allocator_arg, *__a._M_a) { }
  59. template<typename _Alloc>
  60. _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
  61. : _Head(*__a._M_a) { }
  62. template<typename _UHead>
  63. _Head_base(__uses_alloc0, _UHead&& __uhead)
  64. : _Head(std::forward<_UHead>(__uhead)) { }
  65. template<typename _Alloc, typename _UHead>
  66. _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
  67. : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
  68. template<typename _Alloc, typename _UHead>
  69. _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
  70. : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
  71. static constexpr _Head&
  72. _M_head(_Head_base& __b) noexcept { return __b; }
  73. static constexpr const _Head&
  74. _M_head(const _Head_base& __b) noexcept { return __b; }
  75. };
  76. template<std::size_t _Idx, typename _Head>
  77. struct _Head_base<_Idx, _Head, false>
  78. {
  79. constexpr _Head_base()
  80. : _M_head_impl() { }
  81. constexpr _Head_base(const _Head& __h)
  82. : _M_head_impl(__h) { }
  83. constexpr _Head_base(const _Head_base&) = default;
  84. constexpr _Head_base(_Head_base&&) = default;
  85. template<typename _UHead>
  86. constexpr _Head_base(_UHead&& __h)
  87. : _M_head_impl(std::forward<_UHead>(__h)) { }
  88. _Head_base(allocator_arg_t, __uses_alloc0)
  89. : _M_head_impl() { }
  90. template<typename _Alloc>
  91. _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
  92. : _M_head_impl(allocator_arg, *__a._M_a) { }
  93. template<typename _Alloc>
  94. _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
  95. : _M_head_impl(*__a._M_a) { }
  96. template<typename _UHead>
  97. _Head_base(__uses_alloc0, _UHead&& __uhead)
  98. : _M_head_impl(std::forward<_UHead>(__uhead)) { }
  99. template<typename _Alloc, typename _UHead>
  100. _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
  101. : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
  102. { }
  103. template<typename _Alloc, typename _UHead>
  104. _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
  105. : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
  106. static constexpr _Head&
  107. _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
  108. static constexpr const _Head&
  109. _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
  110. _Head _M_head_impl;
  111. };
  112. /**
  113. * Contains the actual implementation of the @c tuple template, stored
  114. * as a recursive inheritance hierarchy from the first element (most
  115. * derived class) to the last (least derived class). The @c Idx
  116. * parameter gives the 0-based index of the element stored at this
  117. * point in the hierarchy; we use it to implement a constant-time
  118. * get() operation.
  119. */
  120. template<std::size_t _Idx, typename... _Elements>
  121. struct _Tuple_impl;
  122. template<typename _Tp>
  123. struct __is_empty_non_tuple : is_empty<_Tp> { };
  124. // Using EBO for elements that are tuples causes ambiguous base errors.
  125. template<typename _El0, typename... _El>
  126. struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
  127. // Use the Empty Base-class Optimization for empty, non-final types.
  128. template<typename _Tp>
  129. using __empty_not_final
  130. = typename conditional<__is_final(_Tp), false_type,
  131. __is_empty_non_tuple<_Tp>>::type;
  132. /**
  133. * Recursive tuple implementation. Here we store the @c Head element
  134. * and derive from a @c Tuple_impl containing the remaining elements
  135. * (which contains the @c Tail).
  136. */
  137. template<std::size_t _Idx, typename _Head, typename... _Tail>
  138. struct _Tuple_impl<_Idx, _Head, _Tail...>
  139. : public _Tuple_impl<_Idx + 1, _Tail...>,
  140. private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
  141. {
  142. template<std::size_t, typename...> friend class _Tuple_impl;
  143. typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
  144. typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
  145. static constexpr _Head&
  146. _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  147. static constexpr const _Head&
  148. _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  149. static constexpr _Inherited&
  150. _M_tail(_Tuple_impl& __t) noexcept { return __t; }
  151. static constexpr const _Inherited&
  152. _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
  153. constexpr _Tuple_impl()
  154. : _Inherited(), _Base() { }
  155. explicit
  156. constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
  157. : _Inherited(__tail...), _Base(__head) { }
  158. template<typename _UHead, typename... _UTail, typename = typename
  159. enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
  160. explicit
  161. constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
  162. : _Inherited(std::forward<_UTail>(__tail)...),
  163. _Base(std::forward<_UHead>(__head)) { }
  164. constexpr _Tuple_impl(const _Tuple_impl&) = default;
  165. constexpr
  166. _Tuple_impl(_Tuple_impl&& __in)
  167. noexcept(__and_<is_nothrow_move_constructible<_Head>,
  168. is_nothrow_move_constructible<_Inherited>>::value)
  169. : _Inherited(std::move(_M_tail(__in))),
  170. _Base(std::forward<_Head>(_M_head(__in))) { }
  171. template<typename... _UElements>
  172. constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
  173. : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
  174. _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
  175. template<typename _UHead, typename... _UTails>
  176. constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  177. : _Inherited(std::move
  178. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
  179. _Base(std::forward<_UHead>
  180. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
  181. template<typename _Alloc>
  182. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
  183. : _Inherited(__tag, __a),
  184. _Base(__tag, __use_alloc<_Head>(__a)) { }
  185. template<typename _Alloc>
  186. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  187. const _Head& __head, const _Tail&... __tail)
  188. : _Inherited(__tag, __a, __tail...),
  189. _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
  190. template<typename _Alloc, typename _UHead, typename... _UTail,
  191. typename = typename enable_if<sizeof...(_Tail)
  192. == sizeof...(_UTail)>::type>
  193. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  194. _UHead&& __head, _UTail&&... __tail)
  195. : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
  196. _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  197. std::forward<_UHead>(__head)) { }
  198. template<typename _Alloc>
  199. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  200. const _Tuple_impl& __in)
  201. : _Inherited(__tag, __a, _M_tail(__in)),
  202. _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
  203. template<typename _Alloc>
  204. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  205. _Tuple_impl&& __in)
  206. : _Inherited(__tag, __a, std::move(_M_tail(__in))),
  207. _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  208. std::forward<_Head>(_M_head(__in))) { }
  209. template<typename _Alloc, typename... _UElements>
  210. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  211. const _Tuple_impl<_Idx, _UElements...>& __in)
  212. : _Inherited(__tag, __a,
  213. _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
  214. _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  215. _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
  216. template<typename _Alloc, typename _UHead, typename... _UTails>
  217. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  218. _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  219. : _Inherited(__tag, __a, std::move
  220. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
  221. _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  222. std::forward<_UHead>
  223. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
  224. _Tuple_impl&
  225. operator=(const _Tuple_impl& __in)
  226. {
  227. _M_head(*this) = _M_head(__in);
  228. _M_tail(*this) = _M_tail(__in);
  229. return *this;
  230. }
  231. _Tuple_impl&
  232. operator=(_Tuple_impl&& __in)
  233. noexcept(__and_<is_nothrow_move_assignable<_Head>,
  234. is_nothrow_move_assignable<_Inherited>>::value)
  235. {
  236. _M_head(*this) = std::forward<_Head>(_M_head(__in));
  237. _M_tail(*this) = std::move(_M_tail(__in));
  238. return *this;
  239. }
  240. template<typename... _UElements>
  241. _Tuple_impl&
  242. operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
  243. {
  244. _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
  245. _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
  246. return *this;
  247. }
  248. template<typename _UHead, typename... _UTails>
  249. _Tuple_impl&
  250. operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
  251. {
  252. _M_head(*this) = std::forward<_UHead>
  253. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
  254. _M_tail(*this) = std::move
  255. (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
  256. return *this;
  257. }
  258. protected:
  259. void
  260. _M_swap(_Tuple_impl& __in)
  261. noexcept(__is_nothrow_swappable<_Head>::value
  262. && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
  263. {
  264. using std::swap;
  265. swap(_M_head(*this), _M_head(__in));
  266. _Inherited::_M_swap(_M_tail(__in));
  267. }
  268. };
  269. // Basis case of inheritance recursion.
  270. template<std::size_t _Idx, typename _Head>
  271. struct _Tuple_impl<_Idx, _Head>
  272. : private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
  273. {
  274. template<std::size_t, typename...> friend class _Tuple_impl;
  275. typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
  276. static constexpr _Head&
  277. _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  278. static constexpr const _Head&
  279. _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
  280. constexpr _Tuple_impl()
  281. : _Base() { }
  282. explicit
  283. constexpr _Tuple_impl(const _Head& __head)
  284. : _Base(__head) { }
  285. template<typename _UHead>
  286. explicit
  287. constexpr _Tuple_impl(_UHead&& __head)
  288. : _Base(std::forward<_UHead>(__head)) { }
  289. constexpr _Tuple_impl(const _Tuple_impl&) = default;
  290. constexpr
  291. _Tuple_impl(_Tuple_impl&& __in)
  292. noexcept(is_nothrow_move_constructible<_Head>::value)
  293. : _Base(std::forward<_Head>(_M_head(__in))) { }
  294. template<typename _UHead>
  295. constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
  296. : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
  297. template<typename _UHead>
  298. constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
  299. : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
  300. { }
  301. template<typename _Alloc>
  302. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
  303. : _Base(__tag, __use_alloc<_Head>(__a)) { }
  304. template<typename _Alloc>
  305. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  306. const _Head& __head)
  307. : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
  308. template<typename _Alloc, typename _UHead>
  309. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  310. _UHead&& __head)
  311. : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  312. std::forward<_UHead>(__head)) { }
  313. template<typename _Alloc>
  314. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  315. const _Tuple_impl& __in)
  316. : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
  317. template<typename _Alloc>
  318. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  319. _Tuple_impl&& __in)
  320. : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  321. std::forward<_Head>(_M_head(__in))) { }
  322. template<typename _Alloc, typename _UHead>
  323. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  324. const _Tuple_impl<_Idx, _UHead>& __in)
  325. : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
  326. _Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
  327. template<typename _Alloc, typename _UHead>
  328. _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
  329. _Tuple_impl<_Idx, _UHead>&& __in)
  330. : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
  331. std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
  332. { }
  333. _Tuple_impl&
  334. operator=(const _Tuple_impl& __in)
  335. {
  336. _M_head(*this) = _M_head(__in);
  337. return *this;
  338. }
  339. _Tuple_impl&
  340. operator=(_Tuple_impl&& __in)
  341. noexcept(is_nothrow_move_assignable<_Head>::value)
  342. {
  343. _M_head(*this) = std::forward<_Head>(_M_head(__in));
  344. return *this;
  345. }
  346. template<typename _UHead>
  347. _Tuple_impl&
  348. operator=(const _Tuple_impl<_Idx, _UHead>& __in)
  349. {
  350. _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
  351. return *this;
  352. }
  353. template<typename _UHead>
  354. _Tuple_impl&
  355. operator=(_Tuple_impl<_Idx, _UHead>&& __in)
  356. {
  357. _M_head(*this)
  358. = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
  359. return *this;
  360. }
  361. protected:
  362. void
  363. _M_swap(_Tuple_impl& __in)
  364. noexcept(__is_nothrow_swappable<_Head>::value)
  365. {
  366. using std::swap;
  367. swap(_M_head(*this), _M_head(__in));
  368. }
  369. };
  370. template<typename... _Elements>
  371. class tuple;
  372. // Concept utility functions, reused in conditionally-explicit
  373. // constructors.
  374. template<bool, typename... _Elements>
  375. struct _TC
  376. {
  377. template<typename... _UElements>
  378. static constexpr bool _ConstructibleTuple()
  379. {
  380. return __and_<is_constructible<_Elements, const _UElements&>...>::value;
  381. }
  382. template<typename... _UElements>
  383. static constexpr bool _ImplicitlyConvertibleTuple()
  384. {
  385. return __and_<is_convertible<const _UElements&, _Elements>...>::value;
  386. }
  387. template<typename... _UElements>
  388. static constexpr bool _MoveConstructibleTuple()
  389. {
  390. return __and_<is_constructible<_Elements, _UElements&&>...>::value;
  391. }
  392. template<typename... _UElements>
  393. static constexpr bool _ImplicitlyMoveConvertibleTuple()
  394. {
  395. return __and_<is_convertible<_UElements&&, _Elements>...>::value;
  396. }
  397. template<typename _SrcTuple>
  398. static constexpr bool _NonNestedTuple()
  399. {
  400. return __and_<__not_<is_same<tuple<_Elements...>,
  401. typename remove_cv<
  402. typename remove_reference<_SrcTuple>::type
  403. >::type>>,
  404. __not_<is_convertible<_SrcTuple, _Elements...>>,
  405. __not_<is_constructible<_Elements..., _SrcTuple>>
  406. >::value;
  407. }
  408. template<typename... _UElements>
  409. static constexpr bool _NotSameTuple()
  410. {
  411. return __not_<is_same<tuple<_Elements...>,
  412. typename remove_const<
  413. typename remove_reference<_UElements...>::type
  414. >::type>>::value;
  415. }
  416. };
  417. template<typename... _Elements>
  418. struct _TC<false, _Elements...>
  419. {
  420. template<typename... _UElements>
  421. static constexpr bool _ConstructibleTuple()
  422. {
  423. return false;
  424. }
  425. template<typename... _UElements>
  426. static constexpr bool _ImplicitlyConvertibleTuple()
  427. {
  428. return false;
  429. }
  430. template<typename... _UElements>
  431. static constexpr bool _MoveConstructibleTuple()
  432. {
  433. return false;
  434. }
  435. template<typename... _UElements>
  436. static constexpr bool _ImplicitlyMoveConvertibleTuple()
  437. {
  438. return false;
  439. }
  440. template<typename... _UElements>
  441. static constexpr bool _NonNestedTuple()
  442. {
  443. return true;
  444. }
  445. template<typename... _UElements>
  446. static constexpr bool _NotSameTuple()
  447. {
  448. return true;
  449. }
  450. };
  451. /// Primary class template, tuple
  452. template<typename... _Elements>
  453. class tuple : public _Tuple_impl<0, _Elements...>
  454. {
  455. typedef _Tuple_impl<0, _Elements...> _Inherited;
  456. // Used for constraining the default constructor so
  457. // that it becomes dependent on the constraints.
  458. template<typename _Dummy>
  459. struct _TC2
  460. {
  461. static constexpr bool _DefaultConstructibleTuple()
  462. {
  463. return __and_<is_default_constructible<_Elements>...>::value;
  464. }
  465. static constexpr bool _ImplicitlyDefaultConstructibleTuple()
  466. {
  467. return __and_<__is_implicitly_default_constructible<_Elements>...>
  468. ::value;
  469. }
  470. };
  471. public:
  472. template<typename _Dummy = void,
  473. typename enable_if<_TC2<_Dummy>::
  474. _ImplicitlyDefaultConstructibleTuple(),
  475. bool>::type = true>
  476. constexpr tuple()
  477. : _Inherited() { }
  478. template<typename _Dummy = void,
  479. typename enable_if<_TC2<_Dummy>::
  480. _DefaultConstructibleTuple()
  481. &&
  482. !_TC2<_Dummy>::
  483. _ImplicitlyDefaultConstructibleTuple(),
  484. bool>::type = false>
  485. explicit constexpr tuple()
  486. : _Inherited() { }
  487. // Shortcut for the cases where constructors taking _Elements...
  488. // need to be constrained.
  489. template<typename _Dummy> using _TCC =
  490. _TC<is_same<_Dummy, void>::value,
  491. _Elements...>;
  492. template<typename _Dummy = void,
  493. typename enable_if<
  494. _TCC<_Dummy>::template
  495. _ConstructibleTuple<_Elements...>()
  496. && _TCC<_Dummy>::template
  497. _ImplicitlyConvertibleTuple<_Elements...>()
  498. && (sizeof...(_Elements) >= 1),
  499. bool>::type=true>
  500. constexpr tuple(const _Elements&... __elements)
  501. : _Inherited(__elements...) { }
  502. template<typename _Dummy = void,
  503. typename enable_if<
  504. _TCC<_Dummy>::template
  505. _ConstructibleTuple<_Elements...>()
  506. && !_TCC<_Dummy>::template
  507. _ImplicitlyConvertibleTuple<_Elements...>()
  508. && (sizeof...(_Elements) >= 1),
  509. bool>::type=false>
  510. explicit constexpr tuple(const _Elements&... __elements)
  511. : _Inherited(__elements...) { }
  512. // Shortcut for the cases where constructors taking _UElements...
  513. // need to be constrained.
  514. template<typename... _UElements> using _TMC =
  515. _TC<(sizeof...(_Elements) == sizeof...(_UElements)),
  516. _Elements...>;
  517. template<typename... _UElements, typename
  518. enable_if<
  519. _TC<sizeof...(_UElements) == 1, _Elements...>::template
  520. _NotSameTuple<_UElements...>()
  521. && _TMC<_UElements...>::template
  522. _MoveConstructibleTuple<_UElements...>()
  523. && _TMC<_UElements...>::template
  524. _ImplicitlyMoveConvertibleTuple<_UElements...>()
  525. && (sizeof...(_Elements) >= 1),
  526. bool>::type=true>
  527. constexpr tuple(_UElements&&... __elements)
  528. : _Inherited(std::forward<_UElements>(__elements)...) { }
  529. template<typename... _UElements, typename
  530. enable_if<
  531. _TC<sizeof...(_UElements) == 1, _Elements...>::template
  532. _NotSameTuple<_UElements...>()
  533. && _TMC<_UElements...>::template
  534. _MoveConstructibleTuple<_UElements...>()
  535. && !_TMC<_UElements...>::template
  536. _ImplicitlyMoveConvertibleTuple<_UElements...>()
  537. && (sizeof...(_Elements) >= 1),
  538. bool>::type=false>
  539. explicit constexpr tuple(_UElements&&... __elements)
  540. : _Inherited(std::forward<_UElements>(__elements)...) { }
  541. constexpr tuple(const tuple&) = default;
  542. constexpr tuple(tuple&&) = default;
  543. // Shortcut for the cases where constructors taking tuples
  544. // must avoid creating temporaries.
  545. template<typename _Dummy> using _TNTC =
  546. _TC<is_same<_Dummy, void>::value && sizeof...(_Elements) == 1,
  547. _Elements...>;
  548. template<typename... _UElements, typename _Dummy = void, typename
  549. enable_if<_TMC<_UElements...>::template
  550. _ConstructibleTuple<_UElements...>()
  551. && _TMC<_UElements...>::template
  552. _ImplicitlyConvertibleTuple<_UElements...>()
  553. && _TNTC<_Dummy>::template
  554. _NonNestedTuple<const tuple<_UElements...>&>(),
  555. bool>::type=true>
  556. constexpr tuple(const tuple<_UElements...>& __in)
  557. : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  558. { }
  559. template<typename... _UElements, typename _Dummy = void, typename
  560. enable_if<_TMC<_UElements...>::template
  561. _ConstructibleTuple<_UElements...>()
  562. && !_TMC<_UElements...>::template
  563. _ImplicitlyConvertibleTuple<_UElements...>()
  564. && _TNTC<_Dummy>::template
  565. _NonNestedTuple<const tuple<_UElements...>&>(),
  566. bool>::type=false>
  567. explicit constexpr tuple(const tuple<_UElements...>& __in)
  568. : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  569. { }
  570. template<typename... _UElements, typename _Dummy = void, typename
  571. enable_if<_TMC<_UElements...>::template
  572. _MoveConstructibleTuple<_UElements...>()
  573. && _TMC<_UElements...>::template
  574. _ImplicitlyMoveConvertibleTuple<_UElements...>()
  575. && _TNTC<_Dummy>::template
  576. _NonNestedTuple<tuple<_UElements...>&&>(),
  577. bool>::type=true>
  578. constexpr tuple(tuple<_UElements...>&& __in)
  579. : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
  580. template<typename... _UElements, typename _Dummy = void, typename
  581. enable_if<_TMC<_UElements...>::template
  582. _MoveConstructibleTuple<_UElements...>()
  583. && !_TMC<_UElements...>::template
  584. _ImplicitlyMoveConvertibleTuple<_UElements...>()
  585. && _TNTC<_Dummy>::template
  586. _NonNestedTuple<tuple<_UElements...>&&>(),
  587. bool>::type=false>
  588. explicit constexpr tuple(tuple<_UElements...>&& __in)
  589. : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
  590. // Allocator-extended constructors.
  591. template<typename _Alloc>
  592. tuple(allocator_arg_t __tag, const _Alloc& __a)
  593. : _Inherited(__tag, __a) { }
  594. template<typename _Alloc, typename _Dummy = void,
  595. typename enable_if<
  596. _TCC<_Dummy>::template
  597. _ConstructibleTuple<_Elements...>()
  598. && _TCC<_Dummy>::template
  599. _ImplicitlyConvertibleTuple<_Elements...>(),
  600. bool>::type=true>
  601. tuple(allocator_arg_t __tag, const _Alloc& __a,
  602. const _Elements&... __elements)
  603. : _Inherited(__tag, __a, __elements...) { }
  604. template<typename _Alloc, typename _Dummy = void,
  605. typename enable_if<
  606. _TCC<_Dummy>::template
  607. _ConstructibleTuple<_Elements...>()
  608. && !_TCC<_Dummy>::template
  609. _ImplicitlyConvertibleTuple<_Elements...>(),
  610. bool>::type=false>
  611. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  612. const _Elements&... __elements)
  613. : _Inherited(__tag, __a, __elements...) { }
  614. template<typename _Alloc, typename... _UElements, typename
  615. enable_if<_TMC<_UElements...>::template
  616. _MoveConstructibleTuple<_UElements...>()
  617. && _TMC<_UElements...>::template
  618. _ImplicitlyMoveConvertibleTuple<_UElements...>(),
  619. bool>::type=true>
  620. tuple(allocator_arg_t __tag, const _Alloc& __a,
  621. _UElements&&... __elements)
  622. : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
  623. { }
  624. template<typename _Alloc, typename... _UElements, typename
  625. enable_if<_TMC<_UElements...>::template
  626. _MoveConstructibleTuple<_UElements...>()
  627. && !_TMC<_UElements...>::template
  628. _ImplicitlyMoveConvertibleTuple<_UElements...>(),
  629. bool>::type=false>
  630. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  631. _UElements&&... __elements)
  632. : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
  633. { }
  634. template<typename _Alloc>
  635. tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
  636. : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
  637. template<typename _Alloc>
  638. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
  639. : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
  640. template<typename _Alloc, typename... _UElements, typename
  641. enable_if<_TMC<_UElements...>::template
  642. _ConstructibleTuple<_UElements...>()
  643. && _TMC<_UElements...>::template
  644. _ImplicitlyConvertibleTuple<_UElements...>(),
  645. bool>::type=true>
  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, typename
  652. enable_if<_TMC<_UElements...>::template
  653. _ConstructibleTuple<_UElements...>()
  654. && !_TMC<_UElements...>::template
  655. _ImplicitlyConvertibleTuple<_UElements...>(),
  656. bool>::type=false>
  657. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  658. const tuple<_UElements...>& __in)
  659. : _Inherited(__tag, __a,
  660. static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
  661. { }
  662. template<typename _Alloc, typename... _UElements, typename
  663. enable_if<_TMC<_UElements...>::template
  664. _MoveConstructibleTuple<_UElements...>()
  665. && _TMC<_UElements...>::template
  666. _ImplicitlyMoveConvertibleTuple<_UElements...>(),
  667. bool>::type=true>
  668. tuple(allocator_arg_t __tag, const _Alloc& __a,
  669. tuple<_UElements...>&& __in)
  670. : _Inherited(__tag, __a,
  671. static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
  672. { }
  673. template<typename _Alloc, typename... _UElements, typename
  674. enable_if<_TMC<_UElements...>::template
  675. _MoveConstructibleTuple<_UElements...>()
  676. && !_TMC<_UElements...>::template
  677. _ImplicitlyMoveConvertibleTuple<_UElements...>(),
  678. bool>::type=false>
  679. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  680. tuple<_UElements...>&& __in)
  681. : _Inherited(__tag, __a,
  682. static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
  683. { }
  684. tuple&
  685. operator=(const tuple& __in)
  686. {
  687. static_cast<_Inherited&>(*this) = __in;
  688. return *this;
  689. }
  690. tuple&
  691. operator=(tuple&& __in)
  692. noexcept(is_nothrow_move_assignable<_Inherited>::value)
  693. {
  694. static_cast<_Inherited&>(*this) = std::move(__in);
  695. return *this;
  696. }
  697. template<typename... _UElements, typename = typename
  698. enable_if<sizeof...(_UElements)
  699. == sizeof...(_Elements)>::type>
  700. tuple&
  701. operator=(const tuple<_UElements...>& __in)
  702. {
  703. static_cast<_Inherited&>(*this) = __in;
  704. return *this;
  705. }
  706. template<typename... _UElements, typename = typename
  707. enable_if<sizeof...(_UElements)
  708. == sizeof...(_Elements)>::type>
  709. tuple&
  710. operator=(tuple<_UElements...>&& __in)
  711. {
  712. static_cast<_Inherited&>(*this) = std::move(__in);
  713. return *this;
  714. }
  715. void
  716. swap(tuple& __in)
  717. noexcept(noexcept(__in._M_swap(__in)))
  718. { _Inherited::_M_swap(__in); }
  719. };
  720. // Explicit specialization, zero-element tuple.
  721. template<>
  722. class tuple<>
  723. {
  724. public:
  725. void swap(tuple&) noexcept { /* no-op */ }
  726. };
  727. /// Partial specialization, 2-element tuple.
  728. /// Includes construction and assignment from a pair.
  729. template<typename _T1, typename _T2>
  730. class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
  731. {
  732. typedef _Tuple_impl<0, _T1, _T2> _Inherited;
  733. public:
  734. template <typename _U1 = _T1,
  735. typename _U2 = _T2,
  736. typename enable_if<__and_<
  737. __is_implicitly_default_constructible<_U1>,
  738. __is_implicitly_default_constructible<_U2>>
  739. ::value, bool>::type = true>
  740. constexpr tuple()
  741. : _Inherited() { }
  742. template <typename _U1 = _T1,
  743. typename _U2 = _T2,
  744. typename enable_if<
  745. __and_<
  746. is_default_constructible<_U1>,
  747. is_default_constructible<_U2>,
  748. __not_<
  749. __and_<__is_implicitly_default_constructible<_U1>,
  750. __is_implicitly_default_constructible<_U2>>>>
  751. ::value, bool>::type = false>
  752. explicit constexpr tuple()
  753. : _Inherited() { }
  754. // Shortcut for the cases where constructors taking _T1, _T2
  755. // need to be constrained.
  756. template<typename _Dummy> using _TCC =
  757. _TC<is_same<_Dummy, void>::value, _T1, _T2>;
  758. template<typename _Dummy = void, typename
  759. enable_if<_TCC<_Dummy>::template
  760. _ConstructibleTuple<_T1, _T2>()
  761. && _TCC<_Dummy>::template
  762. _ImplicitlyConvertibleTuple<_T1, _T2>(),
  763. bool>::type = true>
  764. constexpr tuple(const _T1& __a1, const _T2& __a2)
  765. : _Inherited(__a1, __a2) { }
  766. template<typename _Dummy = void, typename
  767. enable_if<_TCC<_Dummy>::template
  768. _ConstructibleTuple<_T1, _T2>()
  769. && !_TCC<_Dummy>::template
  770. _ImplicitlyConvertibleTuple<_T1, _T2>(),
  771. bool>::type = false>
  772. explicit constexpr tuple(const _T1& __a1, const _T2& __a2)
  773. : _Inherited(__a1, __a2) { }
  774. // Shortcut for the cases where constructors taking _U1, _U2
  775. // need to be constrained.
  776. using _TMC = _TC<true, _T1, _T2>;
  777. template<typename _U1, typename _U2, typename
  778. enable_if<_TMC::template
  779. _MoveConstructibleTuple<_U1, _U2>()
  780. && _TMC::template
  781. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  782. bool>::type = true>
  783. constexpr tuple(_U1&& __a1, _U2&& __a2)
  784. : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
  785. template<typename _U1, typename _U2, typename
  786. enable_if<_TMC::template
  787. _MoveConstructibleTuple<_U1, _U2>()
  788. && !_TMC::template
  789. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  790. bool>::type = false>
  791. explicit constexpr tuple(_U1&& __a1, _U2&& __a2)
  792. : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
  793. constexpr tuple(const tuple&) = default;
  794. constexpr tuple(tuple&&) = default;
  795. template<typename _U1, typename _U2, typename
  796. enable_if<_TMC::template
  797. _ConstructibleTuple<_U1, _U2>()
  798. && _TMC::template
  799. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  800. bool>::type = true>
  801. constexpr tuple(const tuple<_U1, _U2>& __in)
  802. : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
  803. template<typename _U1, typename _U2, typename
  804. enable_if<_TMC::template
  805. _ConstructibleTuple<_U1, _U2>()
  806. && !_TMC::template
  807. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  808. bool>::type = false>
  809. explicit constexpr tuple(const tuple<_U1, _U2>& __in)
  810. : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
  811. template<typename _U1, typename _U2, typename
  812. enable_if<_TMC::template
  813. _MoveConstructibleTuple<_U1, _U2>()
  814. && _TMC::template
  815. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  816. bool>::type = true>
  817. constexpr tuple(tuple<_U1, _U2>&& __in)
  818. : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
  819. template<typename _U1, typename _U2, typename
  820. enable_if<_TMC::template
  821. _MoveConstructibleTuple<_U1, _U2>()
  822. && !_TMC::template
  823. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  824. bool>::type = false>
  825. explicit constexpr tuple(tuple<_U1, _U2>&& __in)
  826. : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
  827. template<typename _U1, typename _U2, typename
  828. enable_if<_TMC::template
  829. _ConstructibleTuple<_U1, _U2>()
  830. && _TMC::template
  831. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  832. bool>::type = true>
  833. constexpr tuple(const pair<_U1, _U2>& __in)
  834. : _Inherited(__in.first, __in.second) { }
  835. template<typename _U1, typename _U2, typename
  836. enable_if<_TMC::template
  837. _ConstructibleTuple<_U1, _U2>()
  838. && !_TMC::template
  839. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  840. bool>::type = false>
  841. explicit constexpr tuple(const pair<_U1, _U2>& __in)
  842. : _Inherited(__in.first, __in.second) { }
  843. template<typename _U1, typename _U2, typename
  844. enable_if<_TMC::template
  845. _MoveConstructibleTuple<_U1, _U2>()
  846. && _TMC::template
  847. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  848. bool>::type = true>
  849. constexpr tuple(pair<_U1, _U2>&& __in)
  850. : _Inherited(std::forward<_U1>(__in.first),
  851. std::forward<_U2>(__in.second)) { }
  852. template<typename _U1, typename _U2, typename
  853. enable_if<_TMC::template
  854. _MoveConstructibleTuple<_U1, _U2>()
  855. && !_TMC::template
  856. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  857. bool>::type = false>
  858. explicit constexpr tuple(pair<_U1, _U2>&& __in)
  859. : _Inherited(std::forward<_U1>(__in.first),
  860. std::forward<_U2>(__in.second)) { }
  861. // Allocator-extended constructors.
  862. template<typename _Alloc>
  863. tuple(allocator_arg_t __tag, const _Alloc& __a)
  864. : _Inherited(__tag, __a) { }
  865. template<typename _Alloc, typename _Dummy = void,
  866. typename enable_if<
  867. _TCC<_Dummy>::template
  868. _ConstructibleTuple<_T1, _T2>()
  869. && _TCC<_Dummy>::template
  870. _ImplicitlyConvertibleTuple<_T1, _T2>(),
  871. bool>::type=true>
  872. tuple(allocator_arg_t __tag, const _Alloc& __a,
  873. const _T1& __a1, const _T2& __a2)
  874. : _Inherited(__tag, __a, __a1, __a2) { }
  875. template<typename _Alloc, typename _Dummy = void,
  876. typename enable_if<
  877. _TCC<_Dummy>::template
  878. _ConstructibleTuple<_T1, _T2>()
  879. && !_TCC<_Dummy>::template
  880. _ImplicitlyConvertibleTuple<_T1, _T2>(),
  881. bool>::type=false>
  882. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  883. const _T1& __a1, const _T2& __a2)
  884. : _Inherited(__tag, __a, __a1, __a2) { }
  885. template<typename _Alloc, typename _U1, typename _U2, typename
  886. enable_if<_TMC::template
  887. _MoveConstructibleTuple<_U1, _U2>()
  888. && _TMC::template
  889. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  890. bool>::type = true>
  891. tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
  892. : _Inherited(__tag, __a, std::forward<_U1>(__a1),
  893. std::forward<_U2>(__a2)) { }
  894. template<typename _Alloc, typename _U1, typename _U2, typename
  895. enable_if<_TMC::template
  896. _MoveConstructibleTuple<_U1, _U2>()
  897. && !_TMC::template
  898. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  899. bool>::type = false>
  900. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  901. _U1&& __a1, _U2&& __a2)
  902. : _Inherited(__tag, __a, std::forward<_U1>(__a1),
  903. std::forward<_U2>(__a2)) { }
  904. template<typename _Alloc>
  905. tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
  906. : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
  907. template<typename _Alloc>
  908. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
  909. : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
  910. template<typename _Alloc, typename _U1, typename _U2, typename
  911. enable_if<_TMC::template
  912. _ConstructibleTuple<_U1, _U2>()
  913. && _TMC::template
  914. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  915. bool>::type = true>
  916. tuple(allocator_arg_t __tag, const _Alloc& __a,
  917. const tuple<_U1, _U2>& __in)
  918. : _Inherited(__tag, __a,
  919. static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
  920. { }
  921. template<typename _Alloc, typename _U1, typename _U2, typename
  922. enable_if<_TMC::template
  923. _ConstructibleTuple<_U1, _U2>()
  924. && !_TMC::template
  925. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  926. bool>::type = false>
  927. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  928. const tuple<_U1, _U2>& __in)
  929. : _Inherited(__tag, __a,
  930. static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
  931. { }
  932. template<typename _Alloc, typename _U1, typename _U2, typename
  933. enable_if<_TMC::template
  934. _MoveConstructibleTuple<_U1, _U2>()
  935. && _TMC::template
  936. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  937. bool>::type = true>
  938. tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
  939. : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
  940. { }
  941. template<typename _Alloc, typename _U1, typename _U2, typename
  942. enable_if<_TMC::template
  943. _MoveConstructibleTuple<_U1, _U2>()
  944. && !_TMC::template
  945. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  946. bool>::type = false>
  947. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  948. tuple<_U1, _U2>&& __in)
  949. : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
  950. { }
  951. template<typename _Alloc, typename _U1, typename _U2, typename
  952. enable_if<_TMC::template
  953. _ConstructibleTuple<_U1, _U2>()
  954. && _TMC::template
  955. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  956. bool>::type = true>
  957. tuple(allocator_arg_t __tag, const _Alloc& __a,
  958. const pair<_U1, _U2>& __in)
  959. : _Inherited(__tag, __a, __in.first, __in.second) { }
  960. template<typename _Alloc, typename _U1, typename _U2, typename
  961. enable_if<_TMC::template
  962. _ConstructibleTuple<_U1, _U2>()
  963. && !_TMC::template
  964. _ImplicitlyConvertibleTuple<_U1, _U2>(),
  965. bool>::type = false>
  966. explicit 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, typename
  970. enable_if<_TMC::template
  971. _MoveConstructibleTuple<_U1, _U2>()
  972. && _TMC::template
  973. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  974. bool>::type = true>
  975. tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
  976. : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
  977. std::forward<_U2>(__in.second)) { }
  978. template<typename _Alloc, typename _U1, typename _U2, typename
  979. enable_if<_TMC::template
  980. _MoveConstructibleTuple<_U1, _U2>()
  981. && !_TMC::template
  982. _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
  983. bool>::type = false>
  984. explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
  985. pair<_U1, _U2>&& __in)
  986. : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
  987. std::forward<_U2>(__in.second)) { }
  988. tuple&
  989. operator=(const tuple& __in)
  990. {
  991. static_cast<_Inherited&>(*this) = __in;
  992. return *this;
  993. }
  994. tuple&
  995. operator=(tuple&& __in)
  996. noexcept(is_nothrow_move_assignable<_Inherited>::value)
  997. {
  998. static_cast<_Inherited&>(*this) = std::move(__in);
  999. return *this;
  1000. }
  1001. template<typename _U1, typename _U2>
  1002. tuple&
  1003. operator=(const tuple<_U1, _U2>& __in)
  1004. {
  1005. static_cast<_Inherited&>(*this) = __in;
  1006. return *this;
  1007. }
  1008. template<typename _U1, typename _U2>
  1009. tuple&
  1010. operator=(tuple<_U1, _U2>&& __in)
  1011. {
  1012. static_cast<_Inherited&>(*this) = std::move(__in);
  1013. return *this;
  1014. }
  1015. template<typename _U1, typename _U2>
  1016. tuple&
  1017. operator=(const pair<_U1, _U2>& __in)
  1018. {
  1019. this->_M_head(*this) = __in.first;
  1020. this->_M_tail(*this)._M_head(*this) = __in.second;
  1021. return *this;
  1022. }
  1023. template<typename _U1, typename _U2>
  1024. tuple&
  1025. operator=(pair<_U1, _U2>&& __in)
  1026. {
  1027. this->_M_head(*this) = std::forward<_U1>(__in.first);
  1028. this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
  1029. return *this;
  1030. }
  1031. void
  1032. swap(tuple& __in)
  1033. noexcept(noexcept(__in._M_swap(__in)))
  1034. { _Inherited::_M_swap(__in); }
  1035. };
  1036. /**
  1037. * Recursive case for tuple_element: strip off the first element in
  1038. * the tuple and retrieve the (i-1)th element of the remaining tuple.
  1039. */
  1040. template<std::size_t __i, typename _Head, typename... _Tail>
  1041. struct tuple_element<__i, tuple<_Head, _Tail...> >
  1042. : tuple_element<__i - 1, tuple<_Tail...> > { };
  1043. /**
  1044. * Basis case for tuple_element: The first element is the one we're seeking.
  1045. */
  1046. template<typename _Head, typename... _Tail>
  1047. struct tuple_element<0, tuple<_Head, _Tail...> >
  1048. {
  1049. typedef _Head type;
  1050. };
  1051. /// class tuple_size
  1052. template<typename... _Elements>
  1053. struct tuple_size<tuple<_Elements...>>
  1054. : public integral_constant<std::size_t, sizeof...(_Elements)> { };
  1055. template<std::size_t __i, typename _Head, typename... _Tail>
  1056. constexpr _Head&
  1057. __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1058. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1059. template<std::size_t __i, typename _Head, typename... _Tail>
  1060. constexpr const _Head&
  1061. __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1062. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1063. /// Return a reference to the ith element of a tuple.
  1064. template<std::size_t __i, typename... _Elements>
  1065. constexpr __tuple_element_t<__i, tuple<_Elements...>>&
  1066. get(tuple<_Elements...>& __t) noexcept
  1067. { return std::__get_helper<__i>(__t); }
  1068. /// Return a const reference to the ith element of a const tuple.
  1069. template<std::size_t __i, typename... _Elements>
  1070. constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
  1071. get(const tuple<_Elements...>& __t) noexcept
  1072. { return std::__get_helper<__i>(__t); }
  1073. /// Return an rvalue reference to the ith element of a tuple rvalue.
  1074. template<std::size_t __i, typename... _Elements>
  1075. constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
  1076. get(tuple<_Elements...>&& __t) noexcept
  1077. {
  1078. typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
  1079. return std::forward<__element_type&&>(std::get<__i>(__t));
  1080. }
  1081. #if __cplusplus > 201103L
  1082. #define __cpp_lib_tuples_by_type 201304
  1083. template<typename _Head, size_t __i, typename... _Tail>
  1084. constexpr _Head&
  1085. __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1086. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1087. template<typename _Head, size_t __i, typename... _Tail>
  1088. constexpr const _Head&
  1089. __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
  1090. { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
  1091. /// Return a reference to the unique element of type _Tp of a tuple.
  1092. template <typename _Tp, typename... _Types>
  1093. constexpr _Tp&
  1094. get(tuple<_Types...>& __t) noexcept
  1095. { return std::__get_helper2<_Tp>(__t); }
  1096. /// Return a reference to the unique element of type _Tp of a tuple rvalue.
  1097. template <typename _Tp, typename... _Types>
  1098. constexpr _Tp&&
  1099. get(tuple<_Types...>&& __t) noexcept
  1100. { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
  1101. /// Return a const reference to the unique element of type _Tp of a tuple.
  1102. template <typename _Tp, typename... _Types>
  1103. constexpr const _Tp&
  1104. get(const tuple<_Types...>& __t) noexcept
  1105. { return std::__get_helper2<_Tp>(__t); }
  1106. #endif
  1107. // This class performs the comparison operations on tuples
  1108. template<typename _Tp, typename _Up, size_t __i, size_t __size>
  1109. struct __tuple_compare
  1110. {
  1111. static constexpr bool
  1112. __eq(const _Tp& __t, const _Up& __u)
  1113. {
  1114. return bool(std::get<__i>(__t) == std::get<__i>(__u))
  1115. && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
  1116. }
  1117. static constexpr bool
  1118. __less(const _Tp& __t, const _Up& __u)
  1119. {
  1120. return bool(std::get<__i>(__t) < std::get<__i>(__u))
  1121. || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
  1122. && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
  1123. }
  1124. };
  1125. template<typename _Tp, typename _Up, size_t __size>
  1126. struct __tuple_compare<_Tp, _Up, __size, __size>
  1127. {
  1128. static constexpr bool
  1129. __eq(const _Tp&, const _Up&) { return true; }
  1130. static constexpr bool
  1131. __less(const _Tp&, const _Up&) { return false; }
  1132. };
  1133. template<typename... _TElements, typename... _UElements>
  1134. constexpr bool
  1135. operator==(const tuple<_TElements...>& __t,
  1136. const tuple<_UElements...>& __u)
  1137. {
  1138. static_assert(sizeof...(_TElements) == sizeof...(_UElements),
  1139. "tuple objects can only be compared if they have equal sizes.");
  1140. using __compare = __tuple_compare<tuple<_TElements...>,
  1141. tuple<_UElements...>,
  1142. 0, sizeof...(_TElements)>;
  1143. return __compare::__eq(__t, __u);
  1144. }
  1145. template<typename... _TElements, typename... _UElements>
  1146. constexpr bool
  1147. operator<(const tuple<_TElements...>& __t,
  1148. const tuple<_UElements...>& __u)
  1149. {
  1150. static_assert(sizeof...(_TElements) == sizeof...(_UElements),
  1151. "tuple objects can only be compared if they have equal sizes.");
  1152. using __compare = __tuple_compare<tuple<_TElements...>,
  1153. tuple<_UElements...>,
  1154. 0, sizeof...(_TElements)>;
  1155. return __compare::__less(__t, __u);
  1156. }
  1157. template<typename... _TElements, typename... _UElements>
  1158. constexpr bool
  1159. operator!=(const tuple<_TElements...>& __t,
  1160. const tuple<_UElements...>& __u)
  1161. { return !(__t == __u); }
  1162. template<typename... _TElements, typename... _UElements>
  1163. constexpr bool
  1164. operator>(const tuple<_TElements...>& __t,
  1165. const tuple<_UElements...>& __u)
  1166. { return __u < __t; }
  1167. template<typename... _TElements, typename... _UElements>
  1168. constexpr bool
  1169. operator<=(const tuple<_TElements...>& __t,
  1170. const tuple<_UElements...>& __u)
  1171. { return !(__u < __t); }
  1172. template<typename... _TElements, typename... _UElements>
  1173. constexpr bool
  1174. operator>=(const tuple<_TElements...>& __t,
  1175. const tuple<_UElements...>& __u)
  1176. { return !(__t < __u); }
  1177. // NB: DR 705.
  1178. template<typename... _Elements>
  1179. constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
  1180. make_tuple(_Elements&&... __args)
  1181. {
  1182. typedef tuple<typename __decay_and_strip<_Elements>::__type...>
  1183. __result_type;
  1184. return __result_type(std::forward<_Elements>(__args)...);
  1185. }
  1186. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1187. // 2275. Why is forward_as_tuple not constexpr?
  1188. template<typename... _Elements>
  1189. constexpr tuple<_Elements&&...>
  1190. forward_as_tuple(_Elements&&... __args) noexcept
  1191. { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
  1192. template<typename... _Tps>
  1193. struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
  1194. { };
  1195. // Internal type trait that allows us to sfinae-protect tuple_cat.
  1196. template<typename _Tp>
  1197. struct __is_tuple_like
  1198. : public __is_tuple_like_impl<typename std::remove_cv
  1199. <typename std::remove_reference<_Tp>::type>::type>::type
  1200. { };
  1201. template<size_t, typename, typename, size_t>
  1202. struct __make_tuple_impl;
  1203. template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
  1204. struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
  1205. : __make_tuple_impl<_Idx + 1,
  1206. tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
  1207. _Tuple, _Nm>
  1208. { };
  1209. template<std::size_t _Nm, typename _Tuple, typename... _Tp>
  1210. struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
  1211. {
  1212. typedef tuple<_Tp...> __type;
  1213. };
  1214. template<typename _Tuple>
  1215. struct __do_make_tuple
  1216. : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
  1217. { };
  1218. // Returns the std::tuple equivalent of a tuple-like type.
  1219. template<typename _Tuple>
  1220. struct __make_tuple
  1221. : public __do_make_tuple<typename std::remove_cv
  1222. <typename std::remove_reference<_Tuple>::type>::type>
  1223. { };
  1224. // Combines several std::tuple's into a single one.
  1225. template<typename...>
  1226. struct __combine_tuples;
  1227. template<>
  1228. struct __combine_tuples<>
  1229. {
  1230. typedef tuple<> __type;
  1231. };
  1232. template<typename... _Ts>
  1233. struct __combine_tuples<tuple<_Ts...>>
  1234. {
  1235. typedef tuple<_Ts...> __type;
  1236. };
  1237. template<typename... _T1s, typename... _T2s, typename... _Rem>
  1238. struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
  1239. {
  1240. typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
  1241. _Rem...>::__type __type;
  1242. };
  1243. // Computes the result type of tuple_cat given a set of tuple-like types.
  1244. template<typename... _Tpls>
  1245. struct __tuple_cat_result
  1246. {
  1247. typedef typename __combine_tuples
  1248. <typename __make_tuple<_Tpls>::__type...>::__type __type;
  1249. };
  1250. // Helper to determine the index set for the first tuple-like
  1251. // type of a given set.
  1252. template<typename...>
  1253. struct __make_1st_indices;
  1254. template<>
  1255. struct __make_1st_indices<>
  1256. {
  1257. typedef std::_Index_tuple<> __type;
  1258. };
  1259. template<typename _Tp, typename... _Tpls>
  1260. struct __make_1st_indices<_Tp, _Tpls...>
  1261. {
  1262. typedef typename std::_Build_index_tuple<std::tuple_size<
  1263. typename std::remove_reference<_Tp>::type>::value>::__type __type;
  1264. };
  1265. // Performs the actual concatenation by step-wise expanding tuple-like
  1266. // objects into the elements, which are finally forwarded into the
  1267. // result tuple.
  1268. template<typename _Ret, typename _Indices, typename... _Tpls>
  1269. struct __tuple_concater;
  1270. template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
  1271. struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
  1272. {
  1273. template<typename... _Us>
  1274. static constexpr _Ret
  1275. _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
  1276. {
  1277. typedef typename __make_1st_indices<_Tpls...>::__type __idx;
  1278. typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
  1279. return __next::_S_do(std::forward<_Tpls>(__tps)...,
  1280. std::forward<_Us>(__us)...,
  1281. std::get<_Is>(std::forward<_Tp>(__tp))...);
  1282. }
  1283. };
  1284. template<typename _Ret>
  1285. struct __tuple_concater<_Ret, std::_Index_tuple<>>
  1286. {
  1287. template<typename... _Us>
  1288. static constexpr _Ret
  1289. _S_do(_Us&&... __us)
  1290. {
  1291. return _Ret(std::forward<_Us>(__us)...);
  1292. }
  1293. };
  1294. /// tuple_cat
  1295. template<typename... _Tpls, typename = typename
  1296. enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
  1297. constexpr auto
  1298. tuple_cat(_Tpls&&... __tpls)
  1299. -> typename __tuple_cat_result<_Tpls...>::__type
  1300. {
  1301. typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
  1302. typedef typename __make_1st_indices<_Tpls...>::__type __idx;
  1303. typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
  1304. return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
  1305. }
  1306. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1307. // 2301. Why is tie not constexpr?
  1308. /// tie
  1309. template<typename... _Elements>
  1310. constexpr tuple<_Elements&...>
  1311. tie(_Elements&... __args) noexcept
  1312. { return tuple<_Elements&...>(__args...); }
  1313. /// swap
  1314. template<typename... _Elements>
  1315. inline void
  1316. swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
  1317. noexcept(noexcept(__x.swap(__y)))
  1318. { __x.swap(__y); }
  1319. // A class (and instance) which can be used in 'tie' when an element
  1320. // of a tuple is not required
  1321. struct _Swallow_assign
  1322. {
  1323. template<class _Tp>
  1324. const _Swallow_assign&
  1325. operator=(const _Tp&) const
  1326. { return *this; }
  1327. };
  1328. const _Swallow_assign ignore{};
  1329. /// Partial specialization for tuples
  1330. template<typename... _Types, typename _Alloc>
  1331. struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
  1332. // See stl_pair.h...
  1333. template<class _T1, class _T2>
  1334. template<typename... _Args1, typename... _Args2>
  1335. inline
  1336. pair<_T1, _T2>::
  1337. pair(piecewise_construct_t,
  1338. tuple<_Args1...> __first, tuple<_Args2...> __second)
  1339. : pair(__first, __second,
  1340. typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
  1341. typename _Build_index_tuple<sizeof...(_Args2)>::__type())
  1342. { }
  1343. template<class _T1, class _T2>
  1344. template<typename... _Args1, std::size_t... _Indexes1,
  1345. typename... _Args2, std::size_t... _Indexes2>
  1346. inline
  1347. pair<_T1, _T2>::
  1348. pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
  1349. _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
  1350. : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
  1351. second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
  1352. { }
  1353. /// @}
  1354. _GLIBCXX_END_NAMESPACE_VERSION
  1355. } // namespace std
  1356. #endif // C++11
  1357. #endif // _GLIBCXX_TUPLE