3 Defines `boost::hana::pair`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_PAIR_HPP
11 #define BOOST_HANA_PAIR_HPP
13 #include <boost/hana/fwd/pair.hpp>
15 #include <boost/hana/config.hpp>
16 #include <boost/hana/detail/decay.hpp>
17 #include <boost/hana/detail/ebo.hpp>
18 #include <boost/hana/detail/intrinsics.hpp>
19 #include <boost/hana/detail/operators/adl.hpp>
20 #include <boost/hana/detail/operators/comparable.hpp>
21 #include <boost/hana/detail/operators/orderable.hpp>
22 #include <boost/hana/fwd/core/make.hpp>
23 #include <boost/hana/fwd/first.hpp>
24 #include <boost/hana/fwd/second.hpp>
26 #include <type_traits>
30 namespace boost { namespace hana {
32 template <int> struct pix; // pair index
35 //////////////////////////////////////////////////////////////////////////
37 //////////////////////////////////////////////////////////////////////////
39 template <typename First, typename Second>
40 #ifdef BOOST_HANA_WORKAROUND_MSVC_EMPTYBASE
41 struct __declspec(empty_bases) pair : detail::operators::adl<pair<First, Second>>
43 struct pair : detail::operators::adl<pair<First, Second>>
45 , private detail::ebo<detail::pix<0>, First>
46 , private detail::ebo<detail::pix<1>, Second>
48 // Default constructor
49 template <typename ...dummy, typename = typename std::enable_if<
50 BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, dummy...) &&
51 BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, dummy...)
54 : detail::ebo<detail::pix<0>, First>()
55 , detail::ebo<detail::pix<1>, Second>()
58 // Variadic constructors
59 template <typename ...dummy, typename = typename std::enable_if<
60 BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, First const&, dummy...) &&
61 BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, Second const&, dummy...)
63 constexpr pair(First const& fst, Second const& snd)
64 : detail::ebo<detail::pix<0>, First>(fst)
65 , detail::ebo<detail::pix<1>, Second>(snd)
68 template <typename T, typename U, typename = typename std::enable_if<
69 BOOST_HANA_TT_IS_CONVERTIBLE(T&&, First) &&
70 BOOST_HANA_TT_IS_CONVERTIBLE(U&&, Second)
72 constexpr pair(T&& t, U&& u)
73 : detail::ebo<detail::pix<0>, First>(static_cast<T&&>(t))
74 , detail::ebo<detail::pix<1>, Second>(static_cast<U&&>(u))
78 // Possibly converting copy and move constructors
79 template <typename T, typename U, typename = typename std::enable_if<
80 BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, T const&) &&
81 BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, U const&) &&
82 BOOST_HANA_TT_IS_CONVERTIBLE(T const&, First) &&
83 BOOST_HANA_TT_IS_CONVERTIBLE(U const&, Second)
85 constexpr pair(pair<T, U> const& other)
86 : detail::ebo<detail::pix<0>, First>(hana::first(other))
87 , detail::ebo<detail::pix<1>, Second>(hana::second(other))
90 template <typename T, typename U, typename = typename std::enable_if<
91 BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, T&&) &&
92 BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, U&&) &&
93 BOOST_HANA_TT_IS_CONVERTIBLE(T&&, First) &&
94 BOOST_HANA_TT_IS_CONVERTIBLE(U&&, Second)
96 constexpr pair(pair<T, U>&& other)
97 : detail::ebo<detail::pix<0>, First>(hana::first(static_cast<pair<T, U>&&>(other)))
98 , detail::ebo<detail::pix<1>, Second>(hana::second(static_cast<pair<T, U>&&>(other)))
102 // Copy and move assignment
103 template <typename T, typename U, typename = typename std::enable_if<
104 BOOST_HANA_TT_IS_ASSIGNABLE(First&, T const&) &&
105 BOOST_HANA_TT_IS_ASSIGNABLE(Second&, U const&)
107 constexpr pair& operator=(pair<T, U> const& other) {
108 hana::first(*this) = hana::first(other);
109 hana::second(*this) = hana::second(other);
113 template <typename T, typename U, typename = typename std::enable_if<
114 BOOST_HANA_TT_IS_ASSIGNABLE(First&, T&&) &&
115 BOOST_HANA_TT_IS_ASSIGNABLE(Second&, U&&)
117 constexpr pair& operator=(pair<T, U>&& other) {
118 hana::first(*this) = hana::first(static_cast<pair<T, U>&&>(other));
119 hana::second(*this) = hana::second(static_cast<pair<T, U>&&>(other));
123 // Prevent the compiler from defining the default copy and move
124 // constructors, which interfere with the SFINAE above.
127 friend struct first_impl<pair_tag>;
128 friend struct second_impl<pair_tag>;
129 template <typename F, typename S> friend struct pair;
133 template <typename First, typename Second>
134 struct tag_of<pair<First, Second>> {
135 using type = pair_tag;
138 //////////////////////////////////////////////////////////////////////////
140 //////////////////////////////////////////////////////////////////////////
143 struct comparable_operators<pair_tag> {
144 static constexpr bool value = true;
147 struct orderable_operators<pair_tag> {
148 static constexpr bool value = true;
152 //////////////////////////////////////////////////////////////////////////
154 //////////////////////////////////////////////////////////////////////////
156 struct make_impl<pair_tag> {
157 template <typename F, typename S>
158 static constexpr pair<
159 typename detail::decay<F>::type,
160 typename detail::decay<S>::type
161 > apply(F&& f, S&& s) {
162 return {static_cast<F&&>(f), static_cast<S&&>(s)};
167 struct first_impl<pair_tag> {
168 template <typename First, typename Second>
169 static constexpr decltype(auto) apply(hana::pair<First, Second>& p) {
170 return detail::ebo_get<detail::pix<0>>(
171 static_cast<detail::ebo<detail::pix<0>, First>&>(p)
174 template <typename First, typename Second>
175 static constexpr decltype(auto) apply(hana::pair<First, Second> const& p) {
176 return detail::ebo_get<detail::pix<0>>(
177 static_cast<detail::ebo<detail::pix<0>, First> const&>(p)
180 template <typename First, typename Second>
181 static constexpr decltype(auto) apply(hana::pair<First, Second>&& p) {
182 return detail::ebo_get<detail::pix<0>>(
183 static_cast<detail::ebo<detail::pix<0>, First>&&>(p)
189 struct second_impl<pair_tag> {
190 template <typename First, typename Second>
191 static constexpr decltype(auto) apply(hana::pair<First, Second>& p) {
192 return detail::ebo_get<detail::pix<1>>(
193 static_cast<detail::ebo<detail::pix<1>, Second>&>(p)
196 template <typename First, typename Second>
197 static constexpr decltype(auto) apply(hana::pair<First, Second> const& p) {
198 return detail::ebo_get<detail::pix<1>>(
199 static_cast<detail::ebo<detail::pix<1>, Second> const&>(p)
202 template <typename First, typename Second>
203 static constexpr decltype(auto) apply(hana::pair<First, Second>&& p) {
204 return detail::ebo_get<detail::pix<1>>(
205 static_cast<detail::ebo<detail::pix<1>, Second>&&>(p)
209 }} // end namespace boost::hana
211 #endif // !BOOST_HANA_PAIR_HPP