Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:04

0001 /*!
0002 @file
0003 Defines `boost::hana::pair`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_PAIR_HPP
0011 #define BOOST_HANA_PAIR_HPP
0012 
0013 #include <boost/hana/fwd/pair.hpp>
0014 
0015 #include <boost/hana/config.hpp>
0016 #include <boost/hana/detail/decay.hpp>
0017 #include <boost/hana/detail/ebo.hpp>
0018 #include <boost/hana/detail/intrinsics.hpp>
0019 #include <boost/hana/detail/operators/adl.hpp>
0020 #include <boost/hana/detail/operators/comparable.hpp>
0021 #include <boost/hana/detail/operators/orderable.hpp>
0022 #include <boost/hana/fwd/core/make.hpp>
0023 #include <boost/hana/fwd/first.hpp>
0024 #include <boost/hana/fwd/second.hpp>
0025 
0026 #include <type_traits>
0027 #include <utility>
0028 
0029 
0030 namespace boost { namespace hana {
0031     namespace detail {
0032         template <int> struct pix; // pair index
0033     }
0034 
0035     //////////////////////////////////////////////////////////////////////////
0036     // pair
0037     //////////////////////////////////////////////////////////////////////////
0038     //! @cond
0039     template <typename First, typename Second>
0040 #ifdef BOOST_HANA_WORKAROUND_MSVC_EMPTYBASE
0041     struct __declspec(empty_bases) pair : detail::operators::adl<pair<First, Second>>
0042 #else
0043     struct pair : detail::operators::adl<pair<First, Second>>
0044 #endif
0045                 , private detail::ebo<detail::pix<0>, First>
0046                 , private detail::ebo<detail::pix<1>, Second>
0047     {
0048         // Default constructor
0049         template <typename ...dummy, typename = typename std::enable_if<
0050             BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, dummy...) &&
0051             BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, dummy...)
0052         >::type>
0053         constexpr pair()
0054             : detail::ebo<detail::pix<0>, First>()
0055             , detail::ebo<detail::pix<1>, Second>()
0056         { }
0057 
0058         // Variadic constructors
0059         template <typename ...dummy, typename = typename std::enable_if<
0060             BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, First const&, dummy...) &&
0061             BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, Second const&, dummy...)
0062         >::type>
0063         constexpr pair(First const& fst, Second const& snd)
0064             : detail::ebo<detail::pix<0>, First>(fst)
0065             , detail::ebo<detail::pix<1>, Second>(snd)
0066         { }
0067 
0068         template <typename T, typename U, typename = typename std::enable_if<
0069             BOOST_HANA_TT_IS_CONVERTIBLE(T&&, First) &&
0070             BOOST_HANA_TT_IS_CONVERTIBLE(U&&, Second)
0071         >::type>
0072         constexpr pair(T&& t, U&& u)
0073             : detail::ebo<detail::pix<0>, First>(static_cast<T&&>(t))
0074             , detail::ebo<detail::pix<1>, Second>(static_cast<U&&>(u))
0075         { }
0076 
0077 
0078         // Possibly converting copy and move constructors
0079         template <typename T, typename U, typename = typename std::enable_if<
0080             BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, T const&) &&
0081             BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, U const&) &&
0082             BOOST_HANA_TT_IS_CONVERTIBLE(T const&, First) &&
0083             BOOST_HANA_TT_IS_CONVERTIBLE(U const&, Second)
0084         >::type>
0085         constexpr pair(pair<T, U> const& other)
0086             : detail::ebo<detail::pix<0>, First>(hana::first(other))
0087             , detail::ebo<detail::pix<1>, Second>(hana::second(other))
0088         { }
0089 
0090         template <typename T, typename U, typename = typename std::enable_if<
0091             BOOST_HANA_TT_IS_CONSTRUCTIBLE(First, T&&) &&
0092             BOOST_HANA_TT_IS_CONSTRUCTIBLE(Second, U&&) &&
0093             BOOST_HANA_TT_IS_CONVERTIBLE(T&&, First) &&
0094             BOOST_HANA_TT_IS_CONVERTIBLE(U&&, Second)
0095         >::type>
0096         constexpr pair(pair<T, U>&& other)
0097             : detail::ebo<detail::pix<0>, First>(hana::first(static_cast<pair<T, U>&&>(other)))
0098             , detail::ebo<detail::pix<1>, Second>(hana::second(static_cast<pair<T, U>&&>(other)))
0099         { }
0100 
0101 
0102         // Copy and move assignment
0103         template <typename T, typename U, typename = typename std::enable_if<
0104             BOOST_HANA_TT_IS_ASSIGNABLE(First&, T const&) &&
0105             BOOST_HANA_TT_IS_ASSIGNABLE(Second&, U const&)
0106         >::type>
0107         constexpr pair& operator=(pair<T, U> const& other) {
0108             hana::first(*this) = hana::first(other);
0109             hana::second(*this) = hana::second(other);
0110             return *this;
0111         }
0112 
0113         template <typename T, typename U, typename = typename std::enable_if<
0114             BOOST_HANA_TT_IS_ASSIGNABLE(First&, T&&) &&
0115             BOOST_HANA_TT_IS_ASSIGNABLE(Second&, U&&)
0116         >::type>
0117         constexpr pair& operator=(pair<T, U>&& other) {
0118             hana::first(*this) = hana::first(static_cast<pair<T, U>&&>(other));
0119             hana::second(*this) = hana::second(static_cast<pair<T, U>&&>(other));
0120             return *this;
0121         }
0122 
0123         // Prevent the compiler from defining the default copy and move
0124         // constructors, which interfere with the SFINAE above.
0125         ~pair() = default;
0126 
0127         friend struct first_impl<pair_tag>;
0128         friend struct second_impl<pair_tag>;
0129         template <typename F, typename S> friend struct pair;
0130     };
0131     //! @endcond
0132 
0133     template <typename First, typename Second>
0134     struct tag_of<pair<First, Second>> {
0135         using type = pair_tag;
0136     };
0137 
0138     //////////////////////////////////////////////////////////////////////////
0139     // Operators
0140     //////////////////////////////////////////////////////////////////////////
0141     namespace detail {
0142         template <>
0143         struct comparable_operators<pair_tag> {
0144             static constexpr bool value = true;
0145         };
0146         template <>
0147         struct orderable_operators<pair_tag> {
0148             static constexpr bool value = true;
0149         };
0150     }
0151 
0152     //////////////////////////////////////////////////////////////////////////
0153     // Product
0154     //////////////////////////////////////////////////////////////////////////
0155     template <>
0156     struct make_impl<pair_tag> {
0157         template <typename F, typename S>
0158         static constexpr pair<
0159             typename detail::decay<F>::type,
0160             typename detail::decay<S>::type
0161         > apply(F&& f, S&& s) {
0162             return {static_cast<F&&>(f), static_cast<S&&>(s)};
0163         }
0164     };
0165 
0166     template <>
0167     struct first_impl<pair_tag> {
0168         template <typename First, typename Second>
0169         static constexpr decltype(auto) apply(hana::pair<First, Second>& p) {
0170             return detail::ebo_get<detail::pix<0>>(
0171                 static_cast<detail::ebo<detail::pix<0>, First>&>(p)
0172             );
0173         }
0174         template <typename First, typename Second>
0175         static constexpr decltype(auto) apply(hana::pair<First, Second> const& p) {
0176             return detail::ebo_get<detail::pix<0>>(
0177                 static_cast<detail::ebo<detail::pix<0>, First> const&>(p)
0178             );
0179         }
0180         template <typename First, typename Second>
0181         static constexpr decltype(auto) apply(hana::pair<First, Second>&& p) {
0182             return detail::ebo_get<detail::pix<0>>(
0183                 static_cast<detail::ebo<detail::pix<0>, First>&&>(p)
0184             );
0185         }
0186     };
0187 
0188     template <>
0189     struct second_impl<pair_tag> {
0190         template <typename First, typename Second>
0191         static constexpr decltype(auto) apply(hana::pair<First, Second>& p) {
0192             return detail::ebo_get<detail::pix<1>>(
0193                 static_cast<detail::ebo<detail::pix<1>, Second>&>(p)
0194             );
0195         }
0196         template <typename First, typename Second>
0197         static constexpr decltype(auto) apply(hana::pair<First, Second> const& p) {
0198             return detail::ebo_get<detail::pix<1>>(
0199                 static_cast<detail::ebo<detail::pix<1>, Second> const&>(p)
0200             );
0201         }
0202         template <typename First, typename Second>
0203         static constexpr decltype(auto) apply(hana::pair<First, Second>&& p) {
0204             return detail::ebo_get<detail::pix<1>>(
0205                 static_cast<detail::ebo<detail::pix<1>, Second>&&>(p)
0206             );
0207         }
0208     };
0209 }} // end namespace boost::hana
0210 
0211 #endif // !BOOST_HANA_PAIR_HPP