stl_pair.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Pair implementation -*- C++ -*-
  2. // Copyright (C) 2001-2018 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. #include <bits/move.h> // for std::move / std::forward, and std::swap
  52. #if __cplusplus >= 201103L
  53. #include <type_traits> // for std::__decay_and_strip too
  54. #endif
  55. namespace std _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. /**
  59. * @addtogroup utilities
  60. * @{
  61. */
  62. #if __cplusplus >= 201103L
  63. /// piecewise_construct_t
  64. struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
  65. /// piecewise_construct
  66. _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
  67. piecewise_construct_t();
  68. // Forward declarations.
  69. template<typename...>
  70. class tuple;
  71. template<std::size_t...>
  72. struct _Index_tuple;
  73. // Concept utility functions, reused in conditionally-explicit
  74. // constructors.
  75. // See PR 70437, don't look at is_constructible or
  76. // is_convertible if the types are the same to
  77. // avoid querying those properties for incomplete types.
  78. template <bool, typename _T1, typename _T2>
  79. struct _PCC
  80. {
  81. template <typename _U1, typename _U2>
  82. static constexpr bool _ConstructiblePair()
  83. {
  84. return __and_<is_constructible<_T1, const _U1&>,
  85. is_constructible<_T2, const _U2&>>::value;
  86. }
  87. template <typename _U1, typename _U2>
  88. static constexpr bool _ImplicitlyConvertiblePair()
  89. {
  90. return __and_<is_convertible<const _U1&, _T1>,
  91. is_convertible<const _U2&, _T2>>::value;
  92. }
  93. template <typename _U1, typename _U2>
  94. static constexpr bool _MoveConstructiblePair()
  95. {
  96. return __and_<is_constructible<_T1, _U1&&>,
  97. is_constructible<_T2, _U2&&>>::value;
  98. }
  99. template <typename _U1, typename _U2>
  100. static constexpr bool _ImplicitlyMoveConvertiblePair()
  101. {
  102. return __and_<is_convertible<_U1&&, _T1>,
  103. is_convertible<_U2&&, _T2>>::value;
  104. }
  105. template <bool __implicit, typename _U1, typename _U2>
  106. static constexpr bool _CopyMovePair()
  107. {
  108. using __do_converts = __and_<is_convertible<const _U1&, _T1>,
  109. is_convertible<_U2&&, _T2>>;
  110. using __converts = typename conditional<__implicit,
  111. __do_converts,
  112. __not_<__do_converts>>::type;
  113. return __and_<is_constructible<_T1, const _U1&>,
  114. is_constructible<_T2, _U2&&>,
  115. __converts
  116. >::value;
  117. }
  118. template <bool __implicit, typename _U1, typename _U2>
  119. static constexpr bool _MoveCopyPair()
  120. {
  121. using __do_converts = __and_<is_convertible<_U1&&, _T1>,
  122. is_convertible<const _U2&, _T2>>;
  123. using __converts = typename conditional<__implicit,
  124. __do_converts,
  125. __not_<__do_converts>>::type;
  126. return __and_<is_constructible<_T1, _U1&&>,
  127. is_constructible<_T2, const _U2&&>,
  128. __converts
  129. >::value;
  130. }
  131. };
  132. template <typename _T1, typename _T2>
  133. struct _PCC<false, _T1, _T2>
  134. {
  135. template <typename _U1, typename _U2>
  136. static constexpr bool _ConstructiblePair()
  137. {
  138. return false;
  139. }
  140. template <typename _U1, typename _U2>
  141. static constexpr bool _ImplicitlyConvertiblePair()
  142. {
  143. return false;
  144. }
  145. template <typename _U1, typename _U2>
  146. static constexpr bool _MoveConstructiblePair()
  147. {
  148. return false;
  149. }
  150. template <typename _U1, typename _U2>
  151. static constexpr bool _ImplicitlyMoveConvertiblePair()
  152. {
  153. return false;
  154. }
  155. };
  156. // PR libstdc++/79141, a utility type for preventing
  157. // initialization of an argument of a disabled assignment
  158. // operator from a pair of empty braces.
  159. struct __nonesuch_no_braces : std::__nonesuch {
  160. explicit __nonesuch_no_braces(const __nonesuch&) = delete;
  161. };
  162. #endif // C++11
  163. template<typename _U1, typename _U2> class __pair_base
  164. {
  165. #if __cplusplus >= 201103L
  166. template<typename _T1, typename _T2> friend struct pair;
  167. __pair_base() = default;
  168. ~__pair_base() = default;
  169. __pair_base(const __pair_base&) = default;
  170. __pair_base& operator=(const __pair_base&) = delete;
  171. #endif // C++11
  172. };
  173. /**
  174. * @brief Struct holding two objects of arbitrary type.
  175. *
  176. * @tparam _T1 Type of first object.
  177. * @tparam _T2 Type of second object.
  178. */
  179. template<typename _T1, typename _T2>
  180. struct pair
  181. : private __pair_base<_T1, _T2>
  182. {
  183. typedef _T1 first_type; /// @c first_type is the first bound type
  184. typedef _T2 second_type; /// @c second_type is the second bound type
  185. _T1 first; /// @c first is a copy of the first object
  186. _T2 second; /// @c second is a copy of the second object
  187. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  188. // 265. std::pair::pair() effects overly restrictive
  189. /** The default constructor creates @c first and @c second using their
  190. * respective default constructors. */
  191. #if __cplusplus >= 201103L
  192. template <typename _U1 = _T1,
  193. typename _U2 = _T2,
  194. typename enable_if<__and_<
  195. __is_implicitly_default_constructible<_U1>,
  196. __is_implicitly_default_constructible<_U2>>
  197. ::value, bool>::type = true>
  198. #endif
  199. _GLIBCXX_CONSTEXPR pair()
  200. : first(), second() { }
  201. #if __cplusplus >= 201103L
  202. template <typename _U1 = _T1,
  203. typename _U2 = _T2,
  204. typename enable_if<__and_<
  205. is_default_constructible<_U1>,
  206. is_default_constructible<_U2>,
  207. __not_<
  208. __and_<__is_implicitly_default_constructible<_U1>,
  209. __is_implicitly_default_constructible<_U2>>>>
  210. ::value, bool>::type = false>
  211. explicit constexpr pair()
  212. : first(), second() { }
  213. #endif
  214. /** Two objects may be passed to a @c pair constructor to be copied. */
  215. #if __cplusplus < 201103L
  216. pair(const _T1& __a, const _T2& __b)
  217. : first(__a), second(__b) { }
  218. #else
  219. // Shortcut for constraining the templates that don't take pairs.
  220. using _PCCP = _PCC<true, _T1, _T2>;
  221. template<typename _U1 = _T1, typename _U2=_T2, typename
  222. enable_if<_PCCP::template
  223. _ConstructiblePair<_U1, _U2>()
  224. && _PCCP::template
  225. _ImplicitlyConvertiblePair<_U1, _U2>(),
  226. bool>::type=true>
  227. constexpr pair(const _T1& __a, const _T2& __b)
  228. : first(__a), second(__b) { }
  229. template<typename _U1 = _T1, typename _U2=_T2, typename
  230. enable_if<_PCCP::template
  231. _ConstructiblePair<_U1, _U2>()
  232. && !_PCCP::template
  233. _ImplicitlyConvertiblePair<_U1, _U2>(),
  234. bool>::type=false>
  235. explicit constexpr pair(const _T1& __a, const _T2& __b)
  236. : first(__a), second(__b) { }
  237. #endif
  238. /** There is also a templated copy ctor for the @c pair class itself. */
  239. #if __cplusplus < 201103L
  240. template<typename _U1, typename _U2>
  241. pair(const pair<_U1, _U2>& __p)
  242. : first(__p.first), second(__p.second) { }
  243. #else
  244. // Shortcut for constraining the templates that take pairs.
  245. template <typename _U1, typename _U2>
  246. using _PCCFP = _PCC<!is_same<_T1, _U1>::value
  247. || !is_same<_T2, _U2>::value,
  248. _T1, _T2>;
  249. template<typename _U1, typename _U2, typename
  250. enable_if<_PCCFP<_U1, _U2>::template
  251. _ConstructiblePair<_U1, _U2>()
  252. && _PCCFP<_U1, _U2>::template
  253. _ImplicitlyConvertiblePair<_U1, _U2>(),
  254. bool>::type=true>
  255. constexpr pair(const pair<_U1, _U2>& __p)
  256. : first(__p.first), second(__p.second) { }
  257. template<typename _U1, typename _U2, typename
  258. enable_if<_PCCFP<_U1, _U2>::template
  259. _ConstructiblePair<_U1, _U2>()
  260. && !_PCCFP<_U1, _U2>::template
  261. _ImplicitlyConvertiblePair<_U1, _U2>(),
  262. bool>::type=false>
  263. explicit constexpr pair(const pair<_U1, _U2>& __p)
  264. : first(__p.first), second(__p.second) { }
  265. constexpr pair(const pair&) = default;
  266. constexpr pair(pair&&) = default;
  267. // DR 811.
  268. template<typename _U1, typename
  269. enable_if<_PCCP::template
  270. _MoveCopyPair<true, _U1, _T2>(),
  271. bool>::type=true>
  272. constexpr pair(_U1&& __x, const _T2& __y)
  273. : first(std::forward<_U1>(__x)), second(__y) { }
  274. template<typename _U1, typename
  275. enable_if<_PCCP::template
  276. _MoveCopyPair<false, _U1, _T2>(),
  277. bool>::type=false>
  278. explicit constexpr pair(_U1&& __x, const _T2& __y)
  279. : first(std::forward<_U1>(__x)), second(__y) { }
  280. template<typename _U2, typename
  281. enable_if<_PCCP::template
  282. _CopyMovePair<true, _T1, _U2>(),
  283. bool>::type=true>
  284. constexpr pair(const _T1& __x, _U2&& __y)
  285. : first(__x), second(std::forward<_U2>(__y)) { }
  286. template<typename _U2, typename
  287. enable_if<_PCCP::template
  288. _CopyMovePair<false, _T1, _U2>(),
  289. bool>::type=false>
  290. explicit pair(const _T1& __x, _U2&& __y)
  291. : first(__x), second(std::forward<_U2>(__y)) { }
  292. template<typename _U1, typename _U2, typename
  293. enable_if<_PCCP::template
  294. _MoveConstructiblePair<_U1, _U2>()
  295. && _PCCP::template
  296. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  297. bool>::type=true>
  298. constexpr pair(_U1&& __x, _U2&& __y)
  299. : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
  300. template<typename _U1, typename _U2, typename
  301. enable_if<_PCCP::template
  302. _MoveConstructiblePair<_U1, _U2>()
  303. && !_PCCP::template
  304. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  305. bool>::type=false>
  306. explicit constexpr pair(_U1&& __x, _U2&& __y)
  307. : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
  308. template<typename _U1, typename _U2, typename
  309. enable_if<_PCCFP<_U1, _U2>::template
  310. _MoveConstructiblePair<_U1, _U2>()
  311. && _PCCFP<_U1, _U2>::template
  312. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  313. bool>::type=true>
  314. constexpr pair(pair<_U1, _U2>&& __p)
  315. : first(std::forward<_U1>(__p.first)),
  316. second(std::forward<_U2>(__p.second)) { }
  317. template<typename _U1, typename _U2, typename
  318. enable_if<_PCCFP<_U1, _U2>::template
  319. _MoveConstructiblePair<_U1, _U2>()
  320. && !_PCCFP<_U1, _U2>::template
  321. _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
  322. bool>::type=false>
  323. explicit constexpr pair(pair<_U1, _U2>&& __p)
  324. : first(std::forward<_U1>(__p.first)),
  325. second(std::forward<_U2>(__p.second)) { }
  326. template<typename... _Args1, typename... _Args2>
  327. pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
  328. pair&
  329. operator=(typename conditional<
  330. __and_<is_copy_assignable<_T1>,
  331. is_copy_assignable<_T2>>::value,
  332. const pair&, const __nonesuch_no_braces&>::type __p)
  333. {
  334. first = __p.first;
  335. second = __p.second;
  336. return *this;
  337. }
  338. pair&
  339. operator=(typename conditional<
  340. __and_<is_move_assignable<_T1>,
  341. is_move_assignable<_T2>>::value,
  342. pair&&, __nonesuch_no_braces&&>::type __p)
  343. noexcept(__and_<is_nothrow_move_assignable<_T1>,
  344. is_nothrow_move_assignable<_T2>>::value)
  345. {
  346. first = std::forward<first_type>(__p.first);
  347. second = std::forward<second_type>(__p.second);
  348. return *this;
  349. }
  350. template<typename _U1, typename _U2>
  351. typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
  352. is_assignable<_T2&, const _U2&>>::value,
  353. pair&>::type
  354. operator=(const pair<_U1, _U2>& __p)
  355. {
  356. first = __p.first;
  357. second = __p.second;
  358. return *this;
  359. }
  360. template<typename _U1, typename _U2>
  361. typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
  362. is_assignable<_T2&, _U2&&>>::value,
  363. pair&>::type
  364. operator=(pair<_U1, _U2>&& __p)
  365. {
  366. first = std::forward<_U1>(__p.first);
  367. second = std::forward<_U2>(__p.second);
  368. return *this;
  369. }
  370. void
  371. swap(pair& __p)
  372. noexcept(__and_<__is_nothrow_swappable<_T1>,
  373. __is_nothrow_swappable<_T2>>::value)
  374. {
  375. using std::swap;
  376. swap(first, __p.first);
  377. swap(second, __p.second);
  378. }
  379. private:
  380. template<typename... _Args1, std::size_t... _Indexes1,
  381. typename... _Args2, std::size_t... _Indexes2>
  382. pair(tuple<_Args1...>&, tuple<_Args2...>&,
  383. _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
  384. #endif
  385. };
  386. #if __cpp_deduction_guides >= 201606
  387. template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
  388. #endif
  389. /// Two pairs of the same type are equal iff their members are equal.
  390. template<typename _T1, typename _T2>
  391. inline _GLIBCXX_CONSTEXPR bool
  392. operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  393. { return __x.first == __y.first && __x.second == __y.second; }
  394. /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
  395. template<typename _T1, typename _T2>
  396. inline _GLIBCXX_CONSTEXPR bool
  397. operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  398. { return __x.first < __y.first
  399. || (!(__y.first < __x.first) && __x.second < __y.second); }
  400. /// Uses @c operator== to find the result.
  401. template<typename _T1, typename _T2>
  402. inline _GLIBCXX_CONSTEXPR bool
  403. operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  404. { return !(__x == __y); }
  405. /// Uses @c operator< to find the result.
  406. template<typename _T1, typename _T2>
  407. inline _GLIBCXX_CONSTEXPR bool
  408. operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  409. { return __y < __x; }
  410. /// Uses @c operator< to find the result.
  411. template<typename _T1, typename _T2>
  412. inline _GLIBCXX_CONSTEXPR bool
  413. operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  414. { return !(__y < __x); }
  415. /// Uses @c operator< to find the result.
  416. template<typename _T1, typename _T2>
  417. inline _GLIBCXX_CONSTEXPR bool
  418. operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  419. { return !(__x < __y); }
  420. #if __cplusplus >= 201103L
  421. /// See std::pair::swap().
  422. // Note: no std::swap overloads in C++03 mode, this has performance
  423. // implications, see, eg, libstdc++/38466.
  424. template<typename _T1, typename _T2>
  425. inline
  426. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  427. // Constrained free swap overload, see p0185r1
  428. typename enable_if<__and_<__is_swappable<_T1>,
  429. __is_swappable<_T2>>::value>::type
  430. #else
  431. void
  432. #endif
  433. swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  434. noexcept(noexcept(__x.swap(__y)))
  435. { __x.swap(__y); }
  436. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  437. template<typename _T1, typename _T2>
  438. typename enable_if<!__and_<__is_swappable<_T1>,
  439. __is_swappable<_T2>>::value>::type
  440. swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
  441. #endif
  442. #endif // __cplusplus >= 201103L
  443. /**
  444. * @brief A convenience wrapper for creating a pair from two objects.
  445. * @param __x The first object.
  446. * @param __y The second object.
  447. * @return A newly-constructed pair<> object of the appropriate type.
  448. *
  449. * The standard requires that the objects be passed by reference-to-const,
  450. * but LWG issue #181 says they should be passed by const value. We follow
  451. * the LWG by default.
  452. */
  453. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  454. // 181. make_pair() unintended behavior
  455. #if __cplusplus >= 201103L
  456. // NB: DR 706.
  457. template<typename _T1, typename _T2>
  458. constexpr pair<typename __decay_and_strip<_T1>::__type,
  459. typename __decay_and_strip<_T2>::__type>
  460. make_pair(_T1&& __x, _T2&& __y)
  461. {
  462. typedef typename __decay_and_strip<_T1>::__type __ds_type1;
  463. typedef typename __decay_and_strip<_T2>::__type __ds_type2;
  464. typedef pair<__ds_type1, __ds_type2> __pair_type;
  465. return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
  466. }
  467. #else
  468. template<typename _T1, typename _T2>
  469. inline pair<_T1, _T2>
  470. make_pair(_T1 __x, _T2 __y)
  471. { return pair<_T1, _T2>(__x, __y); }
  472. #endif
  473. /// @}
  474. _GLIBCXX_END_NAMESPACE_VERSION
  475. } // namespace std
  476. #endif /* _STL_PAIR_H */