stl_pair.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. // Pair implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_pair.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{utility}
  48. */
  49. #ifndef _STL_PAIR_H
  50. #define _STL_PAIR_H 1
  51. #if __cplusplus >= 201103L
  52. # include <type_traits> // for std::__decay_and_strip
  53. # include <bits/move.h> // for std::move / std::forward, and std::swap
  54. # include <bits/utility.h> // for std::tuple_element, std::tuple_size
  55. #endif
  56. #if __cplusplus >= 202002L
  57. # include <compare>
  58. # define __cpp_lib_constexpr_utility 201811L
  59. #endif
  60. namespace std _GLIBCXX_VISIBILITY(default)
  61. {
  62. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  63. /**
  64. * @addtogroup utilities
  65. * @{
  66. */
  67. #if __cplusplus >= 201103L
  68. /// Tag type for piecewise construction of std::pair objects.
  69. struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
  70. /// Tag for piecewise construction of std::pair objects.
  71. _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
  72. piecewise_construct_t();
  73. /// @cond undocumented
  74. // Forward declarations.
  75. template<typename...>
  76. class tuple;
  77. template<size_t...>
  78. struct _Index_tuple;
  79. #if ! __cpp_lib_concepts
  80. // Concept utility functions, reused in conditionally-explicit
  81. // constructors.
  82. // See PR 70437, don't look at is_constructible or
  83. // is_convertible if the types are the same to
  84. // avoid querying those properties for incomplete types.
  85. template <bool, typename _T1, typename _T2>
  86. struct _PCC
  87. {
  88. template <typename _U1, typename _U2>
  89. static constexpr bool _ConstructiblePair()
  90. {
  91. return __and_<is_constructible<_T1, const _U1&>,
  92. is_constructible<_T2, const _U2&>>::value;
  93. }
  94. template <typename _U1, typename _U2>
  95. static constexpr bool _ImplicitlyConvertiblePair()
  96. {
  97. return __and_<is_convertible<const _U1&, _T1>,
  98. is_convertible<const _U2&, _T2>>::value;
  99. }
  100. template <typename _U1, typename _U2>
  101. static constexpr bool _MoveConstructiblePair()
  102. {
  103. return __and_<is_constructible<_T1, _U1&&>,
  104. is_constructible<_T2, _U2&&>>::value;
  105. }
  106. template <typename _U1, typename _U2>
  107. static constexpr bool _ImplicitlyMoveConvertiblePair()
  108. {
  109. return __and_<is_convertible<_U1&&, _T1>,
  110. is_convertible<_U2&&, _T2>>::value;
  111. }
  112. };
  113. template <typename _T1, typename _T2>
  114. struct _PCC<false, _T1, _T2>
  115. {
  116. template <typename _U1, typename _U2>
  117. static constexpr bool _ConstructiblePair()
  118. {
  119. return false;
  120. }
  121. template <typename _U1, typename _U2>
  122. static constexpr bool _ImplicitlyConvertiblePair()
  123. {
  124. return false;
  125. }
  126. template <typename _U1, typename _U2>
  127. static constexpr bool _MoveConstructiblePair()
  128. {
  129. return false;
  130. }
  131. template <typename _U1, typename _U2>
  132. static constexpr bool _ImplicitlyMoveConvertiblePair()
  133. {
  134. return false;
  135. }
  136. };
  137. #endif // lib concepts
  138. #endif // C++11
  139. template<typename _U1, typename _U2> class __pair_base
  140. {
  141. #if __cplusplus >= 201103L && ! __cpp_lib_concepts
  142. template<typename _T1, typename _T2> friend struct pair;
  143. __pair_base() = default;
  144. ~__pair_base() = default;
  145. __pair_base(const __pair_base&) = default;
  146. __pair_base& operator=(const __pair_base&) = delete;
  147. #endif // C++11
  148. };
  149. /// @endcond
  150. /**
  151. * @brief Struct holding two objects of arbitrary type.
  152. *
  153. * @tparam _T1 Type of first object.
  154. * @tparam _T2 Type of second object.
  155. *
  156. * <https://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
  157. */
  158. template<typename _T1, typename _T2>
  159. struct pair
  160. : public __pair_base<_T1, _T2>
  161. {
  162. typedef _T1 first_type; ///< The type of the `first` member
  163. typedef _T2 second_type; ///< The type of the `second` member
  164. _T1 first; ///< The first member
  165. _T2 second; ///< The second member
  166. #if __cplusplus >= 201103L
  167. constexpr pair(const pair&) = default; ///< Copy constructor
  168. constexpr pair(pair&&) = default; ///< Move constructor
  169. template<typename... _Args1, typename... _Args2>
  170. _GLIBCXX20_CONSTEXPR
  171. pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
  172. /// Swap the first members and then the second members.
  173. _GLIBCXX20_CONSTEXPR void
  174. swap(pair& __p)
  175. noexcept(__and_<__is_nothrow_swappable<_T1>,
  176. __is_nothrow_swappable<_T2>>::value)
  177. {
  178. using std::swap;
  179. swap(first, __p.first);
  180. swap(second, __p.second);
  181. }
  182. private:
  183. template<typename... _Args1, size_t... _Indexes1,
  184. typename... _Args2, size_t... _Indexes2>
  185. _GLIBCXX20_CONSTEXPR
  186. pair(tuple<_Args1...>&, tuple<_Args2...>&,
  187. _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
  188. public:
  189. #if __cpp_lib_concepts
  190. // C++20 implementation using concepts, explicit(bool), fully constexpr.
  191. /// Default constructor
  192. constexpr
  193. explicit(__not_<__and_<__is_implicitly_default_constructible<_T1>,
  194. __is_implicitly_default_constructible<_T2>>>())
  195. pair()
  196. requires is_default_constructible_v<_T1>
  197. && is_default_constructible_v<_T2>
  198. : first(), second()
  199. { }
  200. private:
  201. /// @cond undocumented
  202. template<typename _U1, typename _U2>
  203. static constexpr bool
  204. _S_constructible()
  205. {
  206. if constexpr (is_constructible_v<_T1, _U1>)
  207. return is_constructible_v<_T2, _U2>;
  208. return false;
  209. }
  210. template<typename _U1, typename _U2>
  211. static constexpr bool
  212. _S_nothrow_constructible()
  213. {
  214. if constexpr (is_nothrow_constructible_v<_T1, _U1>)
  215. return is_nothrow_constructible_v<_T2, _U2>;
  216. return false;
  217. }
  218. template<typename _U1, typename _U2>
  219. static constexpr bool
  220. _S_convertible()
  221. {
  222. if constexpr (is_convertible_v<_U1, _T1>)
  223. return is_convertible_v<_U2, _T2>;
  224. return false;
  225. }
  226. /// @endcond
  227. public:
  228. /// Constructor accepting lvalues of `first_type` and `second_type`
  229. constexpr explicit(!_S_convertible<const _T1&, const _T2&>())
  230. pair(const _T1& __x, const _T2& __y)
  231. noexcept(_S_nothrow_constructible<const _T1&, const _T2&>())
  232. requires (_S_constructible<const _T1&, const _T2&>())
  233. : first(__x), second(__y)
  234. { }
  235. /// Constructor accepting two values of arbitrary types
  236. template<typename _U1, typename _U2>
  237. requires (_S_constructible<_U1, _U2>())
  238. constexpr explicit(!_S_convertible<_U1, _U2>())
  239. pair(_U1&& __x, _U2&& __y)
  240. noexcept(_S_nothrow_constructible<_U1, _U2>())
  241. : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
  242. { }
  243. /// Converting constructor from a `pair<U1, U2>` lvalue
  244. template<typename _U1, typename _U2>
  245. requires (_S_constructible<const _U1&, const _U2&>())
  246. constexpr explicit(!_S_convertible<const _U1&, const _U2&>())
  247. pair(const pair<_U1, _U2>& __p)
  248. noexcept(_S_nothrow_constructible<const _U1&, const _U2&>())
  249. : first(__p.first), second(__p.second)
  250. { }
  251. /// Converting constructor from a `pair<U1, U2>` rvalue
  252. template<typename _U1, typename _U2>
  253. requires (_S_constructible<_U1, _U2>())
  254. constexpr explicit(!_S_convertible<_U1, _U2>())
  255. pair(pair<_U1, _U2>&& __p)
  256. noexcept(_S_nothrow_constructible<_U1, _U2>())
  257. : first(std::forward<_U1>(__p.first)),
  258. second(std::forward<_U2>(__p.second))
  259. { }
  260. private:
  261. /// @cond undocumented
  262. template<typename _U1, typename _U2>
  263. static constexpr bool
  264. _S_assignable()
  265. {
  266. if constexpr (is_assignable_v<_T1&, _U1>)
  267. return is_assignable_v<_T2&, _U2>;
  268. return false;
  269. }
  270. template<typename _U1, typename _U2>
  271. static constexpr bool
  272. _S_nothrow_assignable()
  273. {
  274. if constexpr (is_nothrow_assignable_v<_T1&, _U1>)
  275. return is_nothrow_assignable_v<_T2&, _U2>;
  276. return false;
  277. }
  278. /// @endcond
  279. public:
  280. pair& operator=(const pair&) = delete;
  281. /// Copy assignment operator
  282. constexpr pair&
  283. operator=(const pair& __p)
  284. noexcept(_S_nothrow_assignable<const _T1&, const _T2&>())
  285. requires (_S_assignable<const _T1&, const _T2&>())
  286. {
  287. first = __p.first;
  288. second = __p.second;
  289. return *this;
  290. }
  291. /// Move assignment operator
  292. constexpr pair&
  293. operator=(pair&& __p)
  294. noexcept(_S_nothrow_assignable<_T1, _T2>())
  295. requires (_S_assignable<_T1, _T2>())
  296. {
  297. first = std::forward<first_type>(__p.first);
  298. second = std::forward<second_type>(__p.second);
  299. return *this;
  300. }
  301. /// Converting assignment from a `pair<U1, U2>` lvalue
  302. template<typename _U1, typename _U2>
  303. constexpr pair&
  304. operator=(const pair<_U1, _U2>& __p)
  305. noexcept(_S_nothrow_assignable<const _U1&, const _U2&>())
  306. requires (_S_assignable<const _U1&, const _U2&>())
  307. {
  308. first = __p.first;
  309. second = __p.second;
  310. return *this;
  311. }
  312. /// Converting assignment from a `pair<U1, U2>` rvalue
  313. template<typename _U1, typename _U2>
  314. constexpr pair&
  315. operator=(pair<_U1, _U2>&& __p)
  316. noexcept(_S_nothrow_assignable<_U1, _U2>())
  317. requires (_S_assignable<_U1, _U2>())
  318. {
  319. first = std::forward<_U1>(__p.first);
  320. second = std::forward<_U2>(__p.second);
  321. return *this;
  322. }
  323. #else
  324. // C++11/14/17 implementation using enable_if, partially constexpr.
  325. /** The default constructor creates @c first and @c second using their
  326. * respective default constructors. */
  327. template <typename _U1 = _T1,
  328. typename _U2 = _T2,
  329. typename enable_if<__and_<
  330. __is_implicitly_default_constructible<_U1>,
  331. __is_implicitly_default_constructible<_U2>>
  332. ::value, bool>::type = true>
  333. constexpr pair()
  334. : first(), second() { }
  335. template <typename _U1 = _T1,
  336. typename _U2 = _T2,
  337. typename enable_if<__and_<
  338. is_default_constructible<_U1>,
  339. is_default_constructible<_U2>,
  340. __not_<
  341. __and_<__is_implicitly_default_constructible<_U1>,
  342. __is_implicitly_default_constructible<_U2>>>>
  343. ::value, bool>::type = false>
  344. explicit constexpr pair()
  345. : first(), second() { }
  346. // Shortcut for constraining the templates that don't take pairs.
  347. /// @cond undocumented
  348. using _PCCP = _PCC<true, _T1, _T2>;
  349. /// @endcond
  350. /// Construct from two const lvalues, allowing implicit conversions.
  351. template<typename _U1 = _T1, typename _U2=_T2, typename
  352. enable_if<_PCCP::template
  353. _ConstructiblePair<_U1, _U2>()
  354. && _PCCP::template
  355. _ImplicitlyConvertiblePair<_U1, _U2>(),
  356. bool>::type=true>
  357. constexpr pair(const _T1& __a, const _T2& __b)
  358. : first(__a), second(__b) { }
  359. /// Construct from two const lvalues, disallowing implicit conversions.
  360. template<typename _U1 = _T1, typename _U2=_T2, typename
  361. enable_if<_PCCP::template
  362. _ConstructiblePair<_U1, _U2>()
  363. && !_PCCP::template
  364. _ImplicitlyConvertiblePair<_U1, _U2>(),
  365. bool>::type=false>
  366. explicit constexpr pair(const _T1& __a, const _T2& __b)
  367. : first(__a), second(__b) { }
  368. // Shortcut for constraining the templates that take pairs.
  369. /// @cond undocumented
  370. template <typename _U1, typename _U2>
  371. using _PCCFP = _PCC<!is_same<_T1, _U1>::value
  372. || !is_same<_T2, _U2>::value,
  373. _T1, _T2>;
  374. /// @endcond
  375. template<typename _U1, typename _U2, typename
  376. enable_if<_PCCFP<_U1, _U2>::template
  377. _ConstructiblePair<_U1, _U2>()
  378. && _PCCFP<_U1, _U2>::template
  379. _ImplicitlyConvertiblePair<_U1, _U2>(),
  380. bool>::type=true>
  381. constexpr pair(const pair<_U1, _U2>& __p)
  382. : first(__p.first), second(__p.second) { }
  383. template<typename _U1, typename _U2, typename
  384. enable_if<_PCCFP<_U1, _U2>::template
  385. _ConstructiblePair<_U1, _U2>()
  386. && !_PCCFP<_U1, _U2>::template
  387. _ImplicitlyConvertiblePair<_U1, _U2>(),
  388. bool>::type=false>
  389. explicit constexpr pair(const pair<_U1, _U2>& __p)
  390. : first(__p.first), second(__p.second) { }
  391. #if _GLIBCXX_USE_DEPRECATED
  392. #if defined(__DEPRECATED)
  393. # define _GLIBCXX_DEPRECATED_PAIR_CTOR \
  394. __attribute__ ((__deprecated__ ("use 'nullptr' instead of '0' to " \
  395. "initialize std::pair of move-only " \
  396. "type and pointer")))
  397. #else
  398. # define _GLIBCXX_DEPRECATED_PAIR_CTOR
  399. #endif
  400. private:
  401. /// @cond undocumented
  402. // A type which can be constructed from literal zero, but not nullptr
  403. struct __zero_as_null_pointer_constant
  404. {
  405. __zero_as_null_pointer_constant(int __zero_as_null_pointer_constant::*)
  406. { }
  407. template<typename _Tp,
  408. typename = __enable_if_t<is_null_pointer<_Tp>::value>>
  409. __zero_as_null_pointer_constant(_Tp) = delete;
  410. };
  411. /// @endcond
  412. public:
  413. // Deprecated extensions to DR 811.
  414. // These allow construction from an rvalue and a literal zero,
  415. // in cases where the standard says the zero should be deduced as int
  416. template<typename _U1,
  417. __enable_if_t<__and_<__not_<is_reference<_U1>>,
  418. is_pointer<_T2>,
  419. is_constructible<_T1, _U1>,
  420. __not_<is_constructible<_T1, const _U1&>>,
  421. is_convertible<_U1, _T1>>::value,
  422. bool> = true>
  423. _GLIBCXX_DEPRECATED_PAIR_CTOR
  424. constexpr
  425. pair(_U1&& __x, __zero_as_null_pointer_constant, ...)
  426. : first(std::forward<_U1>(__x)), second(nullptr) { }
  427. template<typename _U1,
  428. __enable_if_t<__and_<__not_<is_reference<_U1>>,
  429. is_pointer<_T2>,
  430. is_constructible<_T1, _U1>,
  431. __not_<is_constructible<_T1, const _U1&>>,
  432. __not_<is_convertible<_U1, _T1>>>::value,
  433. bool> = false>
  434. _GLIBCXX_DEPRECATED_PAIR_CTOR
  435. explicit constexpr
  436. pair(_U1&& __x, __zero_as_null_pointer_constant, ...)
  437. : first(std::forward<_U1>(__x)), second(nullptr) { }
  438. template<typename _U2,
  439. __enable_if_t<__and_<is_pointer<_T1>,
  440. __not_<is_reference<_U2>>,
  441. is_constructible<_T2, _U2>,
  442. __not_<is_constructible<_T2, const _U2&>>,
  443. is_convertible<_U2, _T2>>::value,
  444. bool> = true>
  445. _GLIBCXX_DEPRECATED_PAIR_CTOR
  446. constexpr
  447. pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
  448. : first(nullptr), second(std::forward<_U2>(__y)) { }
  449. template<typename _U2,
  450. __enable_if_t<__and_<is_pointer<_T1>,
  451. __not_<is_reference<_U2>>,
  452. is_constructible<_T2, _U2>,
  453. __not_<is_constructible<_T2, const _U2&>>,
  454. __not_<is_convertible<_U2, _T2>>>::value,
  455. bool> = false>
  456. _GLIBCXX_DEPRECATED_PAIR_CTOR
  457. explicit constexpr
  458. pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
  459. : first(nullptr), second(std::forward<_U2>(__y)) { }
  460. #undef _GLIBCXX_DEPRECATED_PAIR_CTOR
  461. #endif
  462. template<typename _U1, typename _U2, typename
  463. enable_if<_PCCP::template
  464. _MoveConstructiblePair<_U1, _U2>()
  465. && _PCCP::template
  466. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  467. bool>::type=true>
  468. constexpr pair(_U1&& __x, _U2&& __y)
  469. : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
  470. template<typename _U1, typename _U2, typename
  471. enable_if<_PCCP::template
  472. _MoveConstructiblePair<_U1, _U2>()
  473. && !_PCCP::template
  474. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  475. bool>::type=false>
  476. explicit constexpr pair(_U1&& __x, _U2&& __y)
  477. : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
  478. template<typename _U1, typename _U2, typename
  479. enable_if<_PCCFP<_U1, _U2>::template
  480. _MoveConstructiblePair<_U1, _U2>()
  481. && _PCCFP<_U1, _U2>::template
  482. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  483. bool>::type=true>
  484. constexpr pair(pair<_U1, _U2>&& __p)
  485. : first(std::forward<_U1>(__p.first)),
  486. second(std::forward<_U2>(__p.second)) { }
  487. template<typename _U1, typename _U2, typename
  488. enable_if<_PCCFP<_U1, _U2>::template
  489. _MoveConstructiblePair<_U1, _U2>()
  490. && !_PCCFP<_U1, _U2>::template
  491. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  492. bool>::type=false>
  493. explicit constexpr pair(pair<_U1, _U2>&& __p)
  494. : first(std::forward<_U1>(__p.first)),
  495. second(std::forward<_U2>(__p.second)) { }
  496. pair&
  497. operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
  498. is_copy_assignable<_T2>>::value,
  499. const pair&, const __nonesuch&> __p)
  500. {
  501. first = __p.first;
  502. second = __p.second;
  503. return *this;
  504. }
  505. pair&
  506. operator=(__conditional_t<__and_<is_move_assignable<_T1>,
  507. is_move_assignable<_T2>>::value,
  508. pair&&, __nonesuch&&> __p)
  509. noexcept(__and_<is_nothrow_move_assignable<_T1>,
  510. is_nothrow_move_assignable<_T2>>::value)
  511. {
  512. first = std::forward<first_type>(__p.first);
  513. second = std::forward<second_type>(__p.second);
  514. return *this;
  515. }
  516. template<typename _U1, typename _U2>
  517. typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
  518. is_assignable<_T2&, const _U2&>>::value,
  519. pair&>::type
  520. operator=(const pair<_U1, _U2>& __p)
  521. {
  522. first = __p.first;
  523. second = __p.second;
  524. return *this;
  525. }
  526. template<typename _U1, typename _U2>
  527. typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
  528. is_assignable<_T2&, _U2&&>>::value,
  529. pair&>::type
  530. operator=(pair<_U1, _U2>&& __p)
  531. {
  532. first = std::forward<_U1>(__p.first);
  533. second = std::forward<_U2>(__p.second);
  534. return *this;
  535. }
  536. #endif // lib concepts
  537. #else
  538. // C++03 implementation
  539. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  540. // 265. std::pair::pair() effects overly restrictive
  541. /** The default constructor creates @c first and @c second using their
  542. * respective default constructors. */
  543. pair() : first(), second() { }
  544. /// Two objects may be passed to a `pair` constructor to be copied.
  545. pair(const _T1& __a, const _T2& __b)
  546. : first(__a), second(__b) { }
  547. /// Templated constructor to convert from other pairs.
  548. template<typename _U1, typename _U2>
  549. pair(const pair<_U1, _U2>& __p)
  550. : first(__p.first), second(__p.second) { }
  551. #endif // C++11
  552. };
  553. /// @relates pair @{
  554. #if __cpp_deduction_guides >= 201606
  555. template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
  556. #endif
  557. /// Two pairs of the same type are equal iff their members are equal.
  558. template<typename _T1, typename _T2>
  559. inline _GLIBCXX_CONSTEXPR bool
  560. operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  561. { return __x.first == __y.first && __x.second == __y.second; }
  562. #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
  563. template<typename _T1, typename _T2>
  564. constexpr common_comparison_category_t<__detail::__synth3way_t<_T1>,
  565. __detail::__synth3way_t<_T2>>
  566. operator<=>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  567. {
  568. if (auto __c = __detail::__synth3way(__x.first, __y.first); __c != 0)
  569. return __c;
  570. return __detail::__synth3way(__x.second, __y.second);
  571. }
  572. #else
  573. /** Defines a lexicographical order for pairs.
  574. *
  575. * For two pairs of the same type, `P` is ordered before `Q` if
  576. * `P.first` is less than `Q.first`, or if `P.first` and `Q.first`
  577. * are equivalent (neither is less than the other) and `P.second` is less
  578. * than `Q.second`.
  579. */
  580. template<typename _T1, typename _T2>
  581. inline _GLIBCXX_CONSTEXPR bool
  582. operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  583. { return __x.first < __y.first
  584. || (!(__y.first < __x.first) && __x.second < __y.second); }
  585. /// Uses @c operator== to find the result.
  586. template<typename _T1, typename _T2>
  587. inline _GLIBCXX_CONSTEXPR bool
  588. operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  589. { return !(__x == __y); }
  590. /// Uses @c operator< to find the result.
  591. template<typename _T1, typename _T2>
  592. inline _GLIBCXX_CONSTEXPR bool
  593. operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  594. { return __y < __x; }
  595. /// Uses @c operator< to find the result.
  596. template<typename _T1, typename _T2>
  597. inline _GLIBCXX_CONSTEXPR bool
  598. operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  599. { return !(__y < __x); }
  600. /// Uses @c operator< to find the result.
  601. template<typename _T1, typename _T2>
  602. inline _GLIBCXX_CONSTEXPR bool
  603. operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  604. { return !(__x < __y); }
  605. #endif // !(three_way_comparison && concepts)
  606. #if __cplusplus >= 201103L
  607. /** Swap overload for pairs. Calls std::pair::swap().
  608. *
  609. * @note This std::swap overload is not declared in C++03 mode,
  610. * which has performance implications, e.g. see https://gcc.gnu.org/PR38466
  611. */
  612. template<typename _T1, typename _T2>
  613. _GLIBCXX20_CONSTEXPR inline
  614. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  615. // Constrained free swap overload, see p0185r1
  616. typename enable_if<__and_<__is_swappable<_T1>,
  617. __is_swappable<_T2>>::value>::type
  618. #else
  619. void
  620. #endif
  621. swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  622. noexcept(noexcept(__x.swap(__y)))
  623. { __x.swap(__y); }
  624. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  625. template<typename _T1, typename _T2>
  626. typename enable_if<!__and_<__is_swappable<_T1>,
  627. __is_swappable<_T2>>::value>::type
  628. swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
  629. #endif
  630. #endif // __cplusplus >= 201103L
  631. /// @} relates pair
  632. /**
  633. * @brief A convenience wrapper for creating a pair from two objects.
  634. * @param __x The first object.
  635. * @param __y The second object.
  636. * @return A newly-constructed pair<> object of the appropriate type.
  637. *
  638. * The C++98 standard says the objects are passed by reference-to-const,
  639. * but C++03 says they are passed by value (this was LWG issue #181).
  640. *
  641. * Since C++11 they have been passed by forwarding reference and then
  642. * forwarded to the new members of the pair. To create a pair with a
  643. * member of reference type, pass a `reference_wrapper` to this function.
  644. */
  645. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  646. // 181. make_pair() unintended behavior
  647. #if __cplusplus >= 201103L
  648. // NB: DR 706.
  649. template<typename _T1, typename _T2>
  650. constexpr pair<typename __decay_and_strip<_T1>::__type,
  651. typename __decay_and_strip<_T2>::__type>
  652. make_pair(_T1&& __x, _T2&& __y)
  653. {
  654. typedef typename __decay_and_strip<_T1>::__type __ds_type1;
  655. typedef typename __decay_and_strip<_T2>::__type __ds_type2;
  656. typedef pair<__ds_type1, __ds_type2> __pair_type;
  657. return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
  658. }
  659. #else
  660. template<typename _T1, typename _T2>
  661. inline pair<_T1, _T2>
  662. make_pair(_T1 __x, _T2 __y)
  663. { return pair<_T1, _T2>(__x, __y); }
  664. #endif
  665. /// @}
  666. #if __cplusplus >= 201103L
  667. // Various functions which give std::pair a tuple-like interface.
  668. template<typename _T1, typename _T2>
  669. struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
  670. { };
  671. /// Partial specialization for std::pair
  672. template<class _Tp1, class _Tp2>
  673. struct tuple_size<pair<_Tp1, _Tp2>>
  674. : public integral_constant<size_t, 2> { };
  675. /// Partial specialization for std::pair
  676. template<class _Tp1, class _Tp2>
  677. struct tuple_element<0, pair<_Tp1, _Tp2>>
  678. { typedef _Tp1 type; };
  679. /// Partial specialization for std::pair
  680. template<class _Tp1, class _Tp2>
  681. struct tuple_element<1, pair<_Tp1, _Tp2>>
  682. { typedef _Tp2 type; };
  683. #if __cplusplus >= 201703L
  684. template<typename _Tp1, typename _Tp2>
  685. inline constexpr size_t tuple_size_v<pair<_Tp1, _Tp2>> = 2;
  686. template<typename _Tp1, typename _Tp2>
  687. inline constexpr size_t tuple_size_v<const pair<_Tp1, _Tp2>> = 2;
  688. template<typename _Tp>
  689. inline constexpr bool __is_pair = false;
  690. template<typename _Tp, typename _Up>
  691. inline constexpr bool __is_pair<pair<_Tp, _Up>> = true;
  692. template<typename _Tp, typename _Up>
  693. inline constexpr bool __is_pair<const pair<_Tp, _Up>> = true;
  694. #endif
  695. /// @cond undocumented
  696. template<size_t _Int>
  697. struct __pair_get;
  698. template<>
  699. struct __pair_get<0>
  700. {
  701. template<typename _Tp1, typename _Tp2>
  702. static constexpr _Tp1&
  703. __get(pair<_Tp1, _Tp2>& __pair) noexcept
  704. { return __pair.first; }
  705. template<typename _Tp1, typename _Tp2>
  706. static constexpr _Tp1&&
  707. __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
  708. { return std::forward<_Tp1>(__pair.first); }
  709. template<typename _Tp1, typename _Tp2>
  710. static constexpr const _Tp1&
  711. __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
  712. { return __pair.first; }
  713. template<typename _Tp1, typename _Tp2>
  714. static constexpr const _Tp1&&
  715. __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
  716. { return std::forward<const _Tp1>(__pair.first); }
  717. };
  718. template<>
  719. struct __pair_get<1>
  720. {
  721. template<typename _Tp1, typename _Tp2>
  722. static constexpr _Tp2&
  723. __get(pair<_Tp1, _Tp2>& __pair) noexcept
  724. { return __pair.second; }
  725. template<typename _Tp1, typename _Tp2>
  726. static constexpr _Tp2&&
  727. __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
  728. { return std::forward<_Tp2>(__pair.second); }
  729. template<typename _Tp1, typename _Tp2>
  730. static constexpr const _Tp2&
  731. __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
  732. { return __pair.second; }
  733. template<typename _Tp1, typename _Tp2>
  734. static constexpr const _Tp2&&
  735. __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
  736. { return std::forward<const _Tp2>(__pair.second); }
  737. };
  738. /// @endcond
  739. /** @{
  740. * std::get overloads for accessing members of std::pair
  741. */
  742. template<size_t _Int, class _Tp1, class _Tp2>
  743. constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
  744. get(pair<_Tp1, _Tp2>& __in) noexcept
  745. { return __pair_get<_Int>::__get(__in); }
  746. template<size_t _Int, class _Tp1, class _Tp2>
  747. constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
  748. get(pair<_Tp1, _Tp2>&& __in) noexcept
  749. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  750. template<size_t _Int, class _Tp1, class _Tp2>
  751. constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
  752. get(const pair<_Tp1, _Tp2>& __in) noexcept
  753. { return __pair_get<_Int>::__const_get(__in); }
  754. template<size_t _Int, class _Tp1, class _Tp2>
  755. constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
  756. get(const pair<_Tp1, _Tp2>&& __in) noexcept
  757. { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
  758. #if __cplusplus >= 201402L
  759. #define __cpp_lib_tuples_by_type 201304L
  760. template <typename _Tp, typename _Up>
  761. constexpr _Tp&
  762. get(pair<_Tp, _Up>& __p) noexcept
  763. { return __p.first; }
  764. template <typename _Tp, typename _Up>
  765. constexpr const _Tp&
  766. get(const pair<_Tp, _Up>& __p) noexcept
  767. { return __p.first; }
  768. template <typename _Tp, typename _Up>
  769. constexpr _Tp&&
  770. get(pair<_Tp, _Up>&& __p) noexcept
  771. { return std::move(__p.first); }
  772. template <typename _Tp, typename _Up>
  773. constexpr const _Tp&&
  774. get(const pair<_Tp, _Up>&& __p) noexcept
  775. { return std::move(__p.first); }
  776. template <typename _Tp, typename _Up>
  777. constexpr _Tp&
  778. get(pair<_Up, _Tp>& __p) noexcept
  779. { return __p.second; }
  780. template <typename _Tp, typename _Up>
  781. constexpr const _Tp&
  782. get(const pair<_Up, _Tp>& __p) noexcept
  783. { return __p.second; }
  784. template <typename _Tp, typename _Up>
  785. constexpr _Tp&&
  786. get(pair<_Up, _Tp>&& __p) noexcept
  787. { return std::move(__p.second); }
  788. template <typename _Tp, typename _Up>
  789. constexpr const _Tp&&
  790. get(const pair<_Up, _Tp>&& __p) noexcept
  791. { return std::move(__p.second); }
  792. #endif // C++14
  793. /// @}
  794. #endif // C++11
  795. _GLIBCXX_END_NAMESPACE_VERSION
  796. } // namespace std
  797. #endif /* _STL_PAIR_H */