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::apply`.
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_APPLY_HPP
0011 #define BOOST_HANA_FUNCTIONAL_APPLY_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 
0015 
0016 namespace boost { namespace hana {
0017     //! @ingroup group-functional
0018     //! Invokes a Callable with the given arguments.
0019     //!
0020     //! This is equivalent to [std::invoke][1] that will be added in C++17.
0021     //! However, `apply` is a function object instead of a function, which
0022     //! makes it possible to pass it to higher-order algorithms.
0023     //!
0024     //!
0025     //! @param f
0026     //! A [Callable][2] to be invoked with the given arguments.
0027     //!
0028     //! @param x...
0029     //! The arguments to call `f` with. The number of `x...` must match the
0030     //! arity of `f`.
0031     //!
0032     //!
0033     //! Example
0034     //! -------
0035     //! @include example/functional/apply.cpp
0036     //!
0037     //! [1]: http://en.cppreference.com/w/cpp/utility/functional/invoke
0038     //! [2]: http://en.cppreference.com/w/cpp/named_req/Callable
0039 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0040     constexpr auto apply = [](auto&& f, auto&& ...x) -> decltype(auto) {
0041         return forwarded(f)(forwarded(x)...);
0042     };
0043 #else
0044     struct apply_t {
0045         template <typename F, typename... Args>
0046         constexpr auto operator()(F&& f, Args&&... args) const ->
0047             decltype(static_cast<F&&>(f)(static_cast<Args&&>(args)...))
0048         {
0049             return static_cast<F&&>(f)(static_cast<Args&&>(args)...);
0050         }
0051 
0052         template <typename Base, typename T, typename Derived>
0053         constexpr auto operator()(T Base::*pmd, Derived&& ref) const ->
0054             decltype(static_cast<Derived&&>(ref).*pmd)
0055         {
0056             return static_cast<Derived&&>(ref).*pmd;
0057         }
0058 
0059         template <typename PMD, typename Pointer>
0060         constexpr auto operator()(PMD pmd, Pointer&& ptr) const ->
0061             decltype((*static_cast<Pointer&&>(ptr)).*pmd)
0062         {
0063             return (*static_cast<Pointer&&>(ptr)).*pmd;
0064         }
0065 
0066         template <typename Base, typename T, typename Derived, typename... Args>
0067         constexpr auto operator()(T Base::*pmf, Derived&& ref, Args&&... args) const ->
0068             decltype((static_cast<Derived&&>(ref).*pmf)(static_cast<Args&&>(args)...))
0069         {
0070             return (static_cast<Derived&&>(ref).*pmf)(static_cast<Args&&>(args)...);
0071         }
0072 
0073         template <typename PMF, typename Pointer, typename... Args>
0074         constexpr auto operator()(PMF pmf, Pointer&& ptr, Args&& ...args) const ->
0075             decltype(((*static_cast<Pointer&&>(ptr)).*pmf)(static_cast<Args&&>(args)...))
0076         {
0077             return ((*static_cast<Pointer&&>(ptr)).*pmf)(static_cast<Args&&>(args)...);
0078         }
0079     };
0080 
0081     BOOST_HANA_INLINE_VARIABLE constexpr apply_t apply{};
0082 #endif
0083 }} // end namespace boost::hana
0084 
0085 #endif // !BOOST_HANA_FUNCTIONAL_APPLY_HPP