Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:58

0001 /*!
0002 @file
0003 Defines `boost::hana::overload`.
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_FUNCTIONAL_OVERLOAD_HPP
0011 #define BOOST_HANA_FUNCTIONAL_OVERLOAD_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/detail/decay.hpp>
0015 
0016 
0017 namespace boost { namespace hana {
0018     //! @ingroup group-functional
0019     //! Pick one of several functions to call based on overload resolution.
0020     //!
0021     //! Specifically, `overload(f1, f2, ..., fn)` is a function object such
0022     //! that
0023     //! @code
0024     //!     overload(f1, f2, ..., fn)(x...) == fk(x...)
0025     //! @endcode
0026     //!
0027     //! where `fk` is the function of `f1, ..., fn` that would be called if
0028     //! overload resolution was performed amongst that set of functions only.
0029     //! If more than one function `fk` would be picked by overload resolution,
0030     //! then the call is ambiguous.
0031     //!
0032     //! ### Example
0033     //! @include example/functional/overload.cpp
0034 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0035     constexpr auto overload = [](auto&& f1, auto&& f2, ..., auto&& fn) {
0036         return [perfect-capture](auto&& ...x) -> decltype(auto) {
0037             return forwarded(fk)(forwarded(x)...);
0038         };
0039     };
0040 #else
0041     template <typename F, typename ...G>
0042     struct overload_t
0043         : overload_t<F>::type
0044         , overload_t<G...>::type
0045     {
0046         using type = overload_t;
0047         using overload_t<F>::type::operator();
0048         using overload_t<G...>::type::operator();
0049 
0050         template <typename F_, typename ...G_>
0051         constexpr explicit overload_t(F_&& f, G_&& ...g)
0052             : overload_t<F>::type(static_cast<F_&&>(f))
0053             , overload_t<G...>::type(static_cast<G_&&>(g)...)
0054         { }
0055     };
0056 
0057     template <typename F>
0058     struct overload_t<F> { using type = F; };
0059 
0060     template <typename R, typename ...Args>
0061     struct overload_t<R(*)(Args...)> {
0062         using type = overload_t;
0063         R (*fptr_)(Args...);
0064 
0065         explicit constexpr overload_t(R (*fp)(Args...))
0066             : fptr_(fp)
0067         { }
0068 
0069         constexpr R operator()(Args ...args) const
0070         { return fptr_(static_cast<Args&&>(args)...); }
0071     };
0072 
0073     struct make_overload_t {
0074         template <typename ...F,
0075             typename Overload = typename overload_t<
0076                 typename detail::decay<F>::type...
0077             >::type
0078         >
0079         constexpr Overload operator()(F&& ...f) const {
0080             return Overload(static_cast<F&&>(f)...);
0081         }
0082     };
0083 
0084     BOOST_HANA_INLINE_VARIABLE constexpr make_overload_t overload{};
0085 #endif
0086 }} // end namespace boost::hana
0087 
0088 #endif // !BOOST_HANA_FUNCTIONAL_OVERLOAD_HPP