1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2013.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/container for documentation.
11 //////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_CONTAINER_CONTAINER_DETAIL_PAIR_HPP
14 #define BOOST_CONTAINER_CONTAINER_DETAIL_PAIR_HPP
16 #ifndef BOOST_CONFIG_HPP
17 # include <boost/config.hpp>
20 #if defined(BOOST_HAS_PRAGMA_ONCE)
24 #include <boost/container/detail/config_begin.hpp>
25 #include <boost/container/detail/workaround.hpp>
27 #include <boost/static_assert.hpp>
28 #include <boost/container/detail/mpl.hpp>
29 #include <boost/container/detail/type_traits.hpp>
30 #include <boost/container/detail/mpl.hpp>
31 #include <boost/container/detail/std_fwd.hpp>
32 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
33 # include <boost/container/detail/variadic_templates_tools.hpp>
35 #include <boost/move/adl_move_swap.hpp> //swap
37 #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
38 #include <boost/move/utility_core.hpp>
39 #include <boost/move/detail/fwd_macros.hpp>
47 class T0, class T1, class T2,
48 class T3, class T4, class T5,
49 class T6, class T7, class T8,
53 } //namespace tuples {
60 template <class TupleClass>
63 static const bool value = false;
67 class T0, class T1, class T2,
68 class T3, class T4, class T5,
69 class T6, class T7, class T8,
71 struct is_boost_tuple< boost::tuples::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
73 static const bool value = true;
77 struct disable_if_boost_tuple
78 : boost::container::dtl::disable_if< is_boost_tuple<Tuple> >
84 static const bool value = false;
88 struct is_tuple_null<boost::tuples::null_type>
90 static const bool value = true;
95 #if defined(BOOST_MSVC) && (_CPPLIB_VER == 520)
96 //MSVC 2010 tuple marker
97 namespace std { namespace tr1 { struct _Nil; }}
98 #elif defined(BOOST_MSVC) && (_CPPLIB_VER == 540)
99 //MSVC 2012 tuple marker
100 namespace std { struct _Nil; }
105 namespace container {
107 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
109 template <int Dummy = 0>
110 struct std_piecewise_construct_holder
112 static ::std::piecewise_construct_t *dummy;
116 ::std::piecewise_construct_t *std_piecewise_construct_holder<Dummy>::dummy =
117 reinterpret_cast< ::std::piecewise_construct_t *>(0x01234); //Avoid sanitizer errors on references to null pointers
119 typedef const std::piecewise_construct_t & piecewise_construct_t;
121 struct try_emplace_t{};
125 //! The piecewise_construct_t struct is an empty structure type used as a unique type to
126 //! disambiguate used to disambiguate between different functions that take two tuple arguments.
127 typedef unspecified piecewise_construct_t;
129 #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
131 //! A instance of type
132 //! piecewise_construct_t
133 static piecewise_construct_t piecewise_construct = BOOST_CONTAINER_DOC1ST(unspecified, *std_piecewise_construct_holder<>::dummy);
139 struct piecewise_construct_use
141 //Avoid warnings of unused "piecewise_construct"
142 piecewise_construct_use()
143 { (void)&::boost::container::piecewise_construct; }
146 template <class T1, class T2>
152 static const bool value = false;
155 template <class T1, class T2>
156 struct is_pair< pair<T1, T2> >
158 static const bool value = true;
161 template <class T1, class T2>
162 struct is_pair< std::pair<T1, T2> >
164 static const bool value = true;
170 static const bool value = !is_pair<T>::value;
176 static const bool value = false;
179 template <class T1, class T2>
180 struct is_std_pair< std::pair<T1, T2> >
182 static const bool value = true;
187 template<typename T, typename U, typename V>
188 void get(T); //to enable ADL
192 #ifdef _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
193 //Libc++, in some versions, has an ABI breakage that needs some
194 //padding in dtl::pair, as "std::pair::first" is not at offset zero.
195 //See: https://reviews.llvm.org/D56357 for more information.
197 template <class T1, class T2, std::size_t N>
203 template <class T1, class T2>
204 struct pair_padding<T1, T2, 0>
208 template <class T1, class T2>
217 template <class T1, class T2>
219 #ifdef _LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR
220 : pair_padding<T1, T2, sizeof(std::pair<T1, T2>) - sizeof(simple_pair<T1, T2>)>
224 BOOST_COPYABLE_AND_MOVABLE(pair)
227 typedef T1 first_type;
228 typedef T2 second_type;
233 //Default constructor
237 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
240 //pair copy assignment
242 : first(x.first), second(x.second)
244 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
247 //pair move constructor
248 pair(BOOST_RV_REF(pair) p)
249 : first(::boost::move(p.first)), second(::boost::move(p.second))
251 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
254 template <class D, class S>
255 pair(const pair<D, S> &p)
256 : first(p.first), second(p.second)
258 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
261 template <class D, class S>
262 pair(BOOST_RV_REF_BEG pair<D, S> BOOST_RV_REF_END p)
263 : first(::boost::move(p.first)), second(::boost::move(p.second))
265 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
268 //pair from two values
269 pair(const T1 &t1, const T2 &t2)
273 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
276 template<class U, class V>
277 pair(BOOST_FWD_REF(U) u, BOOST_FWD_REF(V) v)
278 : first(::boost::forward<U>(u))
279 , second(::boost::forward<V>(v))
281 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
284 //And now compatibility with std::pair
285 pair(const std::pair<T1, T2>& x)
286 : first(x.first), second(x.second)
288 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
291 template <class D, class S>
292 pair(const std::pair<D, S>& p)
293 : first(p.first), second(p.second)
295 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
298 pair(BOOST_RV_REF_BEG std::pair<T1, T2> BOOST_RV_REF_END p)
299 : first(::boost::move(p.first)), second(::boost::move(p.second))
301 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
304 template <class D, class S>
305 pair(BOOST_RV_REF_BEG std::pair<D, S> BOOST_RV_REF_END p)
306 : first(::boost::move(p.first)), second(::boost::move(p.second))
308 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
311 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
312 template< class KeyType, class ...Args>
313 pair(try_emplace_t, BOOST_FWD_REF(KeyType) k, Args && ...args)
314 : first(boost::forward<KeyType>(k)), second(::boost::forward<Args>(args)...)\
316 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
320 //piecewise construction from boost::tuple
321 #define BOOST_PAIR_TRY_EMPLACE_CONSTRUCT_CODE(N)\
322 template< class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
323 pair( try_emplace_t, BOOST_FWD_REF(KeyType) k BOOST_MOVE_I##N BOOST_MOVE_UREF##N )\
324 : first(boost::forward<KeyType>(k)), second(BOOST_MOVE_FWD##N)\
326 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));\
329 BOOST_MOVE_ITERATE_0TO9(BOOST_PAIR_TRY_EMPLACE_CONSTRUCT_CODE)
330 #undef BOOST_PAIR_TRY_EMPLACE_CONSTRUCT_CODE
332 #endif //BOOST_NO_CXX11_VARIADIC_TEMPLATES
334 //piecewise construction from boost::tuple
335 #define BOOST_PAIR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE(N,M)\
336 template< template<class, class, class, class, class, class, class, class, class, class> class BoostTuple \
337 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
338 pair( piecewise_construct_t\
339 , BoostTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,N),::boost::tuples::null_type)> p\
340 , BoostTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,M),::boost::tuples::null_type)> q\
341 , typename dtl::enable_if_c\
342 < pair_impl::is_boost_tuple< BoostTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,N),::boost::tuples::null_type)> >::value &&\
343 !(pair_impl::is_tuple_null<BOOST_MOVE_LAST_TARG##N>::value || pair_impl::is_tuple_null<BOOST_MOVE_LAST_TARGQ##M>::value) \
346 : first(BOOST_MOVE_TMPL_GET##N), second(BOOST_MOVE_TMPL_GETQ##M)\
348 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));\
351 BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_PAIR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE)
352 #undef BOOST_PAIR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE
354 //piecewise construction from variadic tuple (with delegating constructors)
355 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
356 # if !defined(BOOST_CONTAINER_NO_CXX11_DELEGATING_CONSTRUCTORS)
358 template<template<class ...> class Tuple, class... Args1, class... Args2, size_t... Indexes1, size_t... Indexes2>
359 pair(Tuple<Args1...>& t1, Tuple<Args2...>& t2, index_tuple<Indexes1...>, index_tuple<Indexes2...>)
360 : first (::boost::forward<Args1>(get<Indexes1>(t1))...)
361 , second(::boost::forward<Args2>(get<Indexes2>(t2))...)
362 { (void) t1; (void)t2; }
365 template< template<class ...> class Tuple, class... Args1, class... Args2
366 , class = typename pair_impl::disable_if_boost_tuple< Tuple<Args1...> >::type>
367 pair(piecewise_construct_t, Tuple<Args1...> t1, Tuple<Args2...> t2)
368 : pair(t1, t2, typename build_number_seq<sizeof...(Args1)>::type(), typename build_number_seq<sizeof...(Args2)>::type())
370 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
373 //piecewise construction from variadic tuple (suboptimal, without delegating constructors)
375 template<typename T, template<class ...> class Tuple, typename... Args>
376 static T build_from_args(Tuple<Args...>&& t)
377 { return do_build_from_args<T>(::boost::move(t), typename build_number_seq<sizeof...(Args)>::type()); }
379 template<typename T, template<class ...> class Tuple, typename... Args, std::size_t... Indexes>
380 static T do_build_from_args(Tuple<Args...> && t, const index_tuple<Indexes...>&)
381 { (void)t; return T(::boost::forward<Args>(get<Indexes>(t))...); }
384 template< template<class ...> class Tuple, class... Args1, class... Args2
385 , class = typename pair_impl::disable_if_boost_tuple< Tuple<Args1...> >::type>
386 pair(piecewise_construct_t, Tuple<Args1...> t1, Tuple<Args2...> t2)
387 : first (build_from_args<first_type> (::boost::move(t1)))
388 , second (build_from_args<second_type>(::boost::move(t2)))
390 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));
392 # endif //BOOST_NO_CXX11_VARIADIC_TEMPLATES
393 #elif defined(BOOST_MSVC) && (_CPPLIB_VER == 520)
394 //MSVC 2010 tuple implementation
395 #define BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE(N,M)\
396 template< template<class, class, class, class, class, class, class, class, class, class> class StdTuple \
397 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
398 pair( piecewise_construct_t\
399 , StdTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,N),::std::tr1::_Nil)> p\
400 , StdTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,M),::std::tr1::_Nil)> q)\
401 : first(BOOST_MOVE_GET_IDX##N), second(BOOST_MOVE_GET_IDXQ##M)\
403 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));\
406 BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE)
407 #undef BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE
408 #elif defined(BOOST_MSVC) && (_CPPLIB_VER == 540)
409 #if _VARIADIC_MAX >= 9
410 #define BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT 9
412 #define BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT BOOST_MOVE_ADD(_VARIADIC_MAX, 1)
415 //MSVC 2012 tuple implementation
416 #define BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_CODE(N,M)\
417 template< template<BOOST_MOVE_REPEAT(_VARIADIC_MAX, class), class, class, class> class StdTuple \
418 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
419 pair( piecewise_construct_t\
420 , StdTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(BOOST_MOVE_ADD(_VARIADIC_MAX, 3),N),::std::_Nil) > p\
421 , StdTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(BOOST_MOVE_ADD(_VARIADIC_MAX, 3),M),::std::_Nil) > q)\
422 : first(BOOST_MOVE_GET_IDX##N), second(BOOST_MOVE_GET_IDXQ##M)\
424 BOOST_STATIC_ASSERT((sizeof(std::pair<T1, T2>) == sizeof(pair<T1, T2>)));\
427 BOOST_MOVE_ITER2D_0TOMAX(BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT, BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_CODE)
428 #undef BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE
429 #undef BOOST_PAIR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT
432 //pair copy assignment
433 pair& operator=(BOOST_COPY_ASSIGN_REF(pair) p)
440 //pair move assignment
441 pair& operator=(BOOST_RV_REF(pair) p)
443 first = ::boost::move(p.first);
444 second = ::boost::move(p.second);
448 template <class D, class S>
449 typename ::boost::container::dtl::disable_if_or
451 , ::boost::container::dtl::is_same<T1, D>
452 , ::boost::container::dtl::is_same<T2, S>
454 operator=(const pair<D, S>&p)
461 template <class D, class S>
462 typename ::boost::container::dtl::disable_if_or
464 , ::boost::container::dtl::is_same<T1, D>
465 , ::boost::container::dtl::is_same<T2, S>
467 operator=(BOOST_RV_REF_BEG pair<D, S> BOOST_RV_REF_END p)
469 first = ::boost::move(p.first);
470 second = ::boost::move(p.second);
473 //std::pair copy assignment
474 pair& operator=(const std::pair<T1, T2> &p)
481 template <class D, class S>
482 pair& operator=(const std::pair<D, S> &p)
484 first = ::boost::move(p.first);
485 second = ::boost::move(p.second);
489 //std::pair move assignment
490 pair& operator=(BOOST_RV_REF_BEG std::pair<T1, T2> BOOST_RV_REF_END p)
492 first = ::boost::move(p.first);
493 second = ::boost::move(p.second);
497 template <class D, class S>
498 pair& operator=(BOOST_RV_REF_BEG std::pair<D, S> BOOST_RV_REF_END p)
500 first = ::boost::move(p.first);
501 second = ::boost::move(p.second);
508 ::boost::adl_move_swap(this->first, p.first);
509 ::boost::adl_move_swap(this->second, p.second);
513 template <class T1, class T2>
514 inline bool operator==(const pair<T1,T2>& x, const pair<T1,T2>& y)
515 { return static_cast<bool>(x.first == y.first && x.second == y.second); }
517 template <class T1, class T2>
518 inline bool operator< (const pair<T1,T2>& x, const pair<T1,T2>& y)
519 { return static_cast<bool>(x.first < y.first ||
520 (!(y.first < x.first) && x.second < y.second)); }
522 template <class T1, class T2>
523 inline bool operator!=(const pair<T1,T2>& x, const pair<T1,T2>& y)
524 { return static_cast<bool>(!(x == y)); }
526 template <class T1, class T2>
527 inline bool operator> (const pair<T1,T2>& x, const pair<T1,T2>& y)
530 template <class T1, class T2>
531 inline bool operator>=(const pair<T1,T2>& x, const pair<T1,T2>& y)
532 { return static_cast<bool>(!(x < y)); }
534 template <class T1, class T2>
535 inline bool operator<=(const pair<T1,T2>& x, const pair<T1,T2>& y)
536 { return static_cast<bool>(!(y < x)); }
538 template <class T1, class T2>
539 inline pair<T1, T2> make_pair(T1 x, T2 y)
540 { return pair<T1, T2>(x, y); }
542 template <class T1, class T2>
543 inline void swap(pair<T1, T2>& x, pair<T1, T2>& y)
547 } //namespace container {
549 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
551 template<class T1, class T2>
552 struct has_move_emulation_enabled< ::boost::container::dtl::pair<T1, T2> >
554 static const bool value = true;
559 namespace move_detail{
562 struct is_class_or_union;
564 template <class T1, class T2>
565 struct is_class_or_union< ::boost::container::dtl::pair<T1, T2> >
566 //This specialization is needed to avoid instantiation of pair in
567 //is_class, and allow recursive maps.
569 static const bool value = true;
572 template <class T1, class T2>
573 struct is_class_or_union< std::pair<T1, T2> >
574 //This specialization is needed to avoid instantiation of pair in
575 //is_class, and allow recursive maps.
577 static const bool value = true;
583 template <class T1, class T2>
584 struct is_union< ::boost::container::dtl::pair<T1, T2> >
585 //This specialization is needed to avoid instantiation of pair in
586 //is_class, and allow recursive maps.
588 static const bool value = false;
591 template <class T1, class T2>
592 struct is_union< std::pair<T1, T2> >
593 //This specialization is needed to avoid instantiation of pair in
594 //is_class, and allow recursive maps.
596 static const bool value = false;
602 template <class T1, class T2>
603 struct is_class< ::boost::container::dtl::pair<T1, T2> >
604 //This specialization is needed to avoid instantiation of pair in
605 //is_class, and allow recursive maps.
607 static const bool value = true;
610 template <class T1, class T2>
611 struct is_class< std::pair<T1, T2> >
612 //This specialization is needed to avoid instantiation of pair in
613 //is_class, and allow recursive maps.
615 static const bool value = true;
621 struct is_trivially_copy_constructible;
623 template<class A, class B>
624 struct is_trivially_copy_assignable
625 <boost::container::dtl::pair<A,B> >
627 static const bool value = boost::move_detail::is_trivially_copy_assignable<A>::value &&
628 boost::move_detail::is_trivially_copy_assignable<B>::value ;
632 struct is_trivially_move_constructible;
634 template<class A, class B>
635 struct is_trivially_move_assignable
636 <boost::container::dtl::pair<A,B> >
638 static const bool value = boost::move_detail::is_trivially_move_assignable<A>::value &&
639 boost::move_detail::is_trivially_move_assignable<B>::value ;
643 struct is_trivially_copy_assignable;
645 template<class A, class B>
646 struct is_trivially_copy_constructible<boost::container::dtl::pair<A,B> >
648 static const bool value = boost::move_detail::is_trivially_copy_constructible<A>::value &&
649 boost::move_detail::is_trivially_copy_constructible<B>::value ;
653 struct is_trivially_move_assignable;
655 template<class A, class B>
656 struct is_trivially_move_constructible<boost::container::dtl::pair<A,B> >
658 static const bool value = boost::move_detail::is_trivially_move_constructible<A>::value &&
659 boost::move_detail::is_trivially_move_constructible<B>::value ;
663 struct is_trivially_destructible;
665 template<class A, class B>
666 struct is_trivially_destructible<boost::container::dtl::pair<A,B> >
668 static const bool value = boost::move_detail::is_trivially_destructible<A>::value &&
669 boost::move_detail::is_trivially_destructible<B>::value ;
673 } //namespace move_detail{
675 } //namespace boost {
677 #include <boost/container/detail/config_end.hpp>
679 #endif //#ifndef BOOST_CONTAINER_DETAIL_PAIR_HPP