/*============================================================================= Copyright (c) 2015 Paul Fultz II apply.h Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #ifndef BOOST_HOF_GUARD_APPLY_H #define BOOST_HOF_GUARD_APPLY_H /// apply /// ===== /// /// Description /// ----------- /// /// The `apply` function calls the function given to it with its arguments. /// /// Synopsis /// -------- /// /// template /// constexpr auto apply(F&& f, Ts&&... xs); /// /// Semantics /// --------- /// /// assert(apply(f)(xs...) == f(xs...)); /// assert(fold(apply, f)(x, y, z) == f(x)(y)(z)); /// /// Requirements /// ------------ /// /// F must be: /// /// * [Invocable](Invocable) /// /// Example /// ------- /// /// #include /// #include /// /// struct sum_f /// { /// template /// T operator()(T x, U y) const /// { /// return x+y; /// } /// }; /// /// int main() { /// assert(boost::hof::apply(sum_f(), 1, 2) == 3); /// } /// #include #include #include #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable: 4003) #endif #define BOOST_HOF_DETAIL_FOREACH_QUAL(m, data) \ m(, data) \ m(const, data) \ m(volatile, data) \ m(const volatile, data) namespace boost { namespace hof { namespace detail { #if BOOST_HOF_HAS_MANUAL_DEDUCTION || BOOST_HOF_NO_EXPRESSION_SFINAE struct apply_mem_fn { template struct convertible_args; template struct is_convertible_args : std::false_type {}; template struct is_convertible_args< convertible_args, convertible_args, typename std::enable_if<( sizeof...(Ts) == sizeof...(Us) )>::type > : and_...> {}; template struct is_compatible : std::is_convertible< typename std::add_pointer::type>::type, typename std::add_pointer::type>::type > {}; #define BOOST_HOF_APPLY_MEM_FN_CALL(cv, data) \ template , \ is_convertible_args, convertible_args> \ >::value>::type> \ constexpr R operator()(R (Base::*mf)(Ts...) cv, Derived&& ref, Us &&... xs) const \ BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT((BOOST_HOF_FORWARD(Derived)(ref).*mf)(BOOST_HOF_FORWARD(Us)(xs)...)) \ { \ return (BOOST_HOF_FORWARD(Derived)(ref).*mf)(BOOST_HOF_FORWARD(Us)(xs)...); \ } BOOST_HOF_DETAIL_FOREACH_QUAL(BOOST_HOF_APPLY_MEM_FN_CALL, ~) }; struct apply_mem_data { template struct match_qualifier { typedef R type; }; #define BOOST_HOF_APPLY_MEM_DATA_MATCH(cv, ref) \ template \ struct match_qualifier \ : match_qualifier \ {}; BOOST_HOF_DETAIL_FOREACH_QUAL(BOOST_HOF_APPLY_MEM_DATA_MATCH,&) BOOST_HOF_DETAIL_FOREACH_QUAL(BOOST_HOF_APPLY_MEM_DATA_MATCH,&&) template ::type>::value )>::type> constexpr typename match_qualifier::type operator()(R Base::*pmd, Derived&& ref) const noexcept { return BOOST_HOF_FORWARD(Derived)(ref).*pmd; } }; template())> struct apply_deref { typedef U type; }; #endif struct apply_f { #if BOOST_HOF_HAS_MANUAL_DEDUCTION || BOOST_HOF_NO_EXPRESSION_SFINAE template::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_fn, id_, id_, id_...) operator()(F&& f, T&& obj, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_fn()(f, BOOST_HOF_FORWARD(T)(obj), BOOST_HOF_FORWARD(Ts)(xs)...) ); template::type, class=typename std::enable_if<( std::is_member_function_pointer::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_fn, id_, id_, id_...) operator()(F&& f, T&& obj, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_fn()(f, *BOOST_HOF_FORWARD(T)(obj), BOOST_HOF_FORWARD(Ts)(xs)...) ); template::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_fn, id_, id_, id_...) operator()(F&& f, const std::reference_wrapper& ref, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_fn()(f, ref.get(), BOOST_HOF_FORWARD(Ts)(xs)...) ); template::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_data, id_, id_) operator()(F&& f, T&& obj) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_data()(f, BOOST_HOF_FORWARD(T)(obj)) ); template::type, class=typename std::enable_if<( std::is_member_object_pointer::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_data, id_, id_) operator()(F&& f, T&& obj) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_data()(f, *BOOST_HOF_FORWARD(T)(obj)) ); template::type>::value )>::type> constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(apply_mem_data, id_, id_) operator()(F&& f, const std::reference_wrapper& ref) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( apply_mem_data()(f, ref.get()) ); #else template constexpr auto operator()(T Base::*pmd, Derived&& ref) const BOOST_HOF_RETURNS(BOOST_HOF_FORWARD(Derived)(ref).*pmd); template constexpr auto operator()(PMD&& pmd, Pointer&& ptr) const BOOST_HOF_RETURNS((*BOOST_HOF_FORWARD(Pointer)(ptr)).*BOOST_HOF_FORWARD(PMD)(pmd)); template constexpr auto operator()(T Base::*pmd, const std::reference_wrapper& ref) const BOOST_HOF_RETURNS(ref.get().*pmd); template constexpr auto operator()(T Base::*pmf, Derived&& ref, Args&&... args) const BOOST_HOF_RETURNS((BOOST_HOF_FORWARD(Derived)(ref).*pmf)(BOOST_HOF_FORWARD(Args)(args)...)); template constexpr auto operator()(PMF&& pmf, Pointer&& ptr, Args&&... args) const BOOST_HOF_RETURNS(((*BOOST_HOF_FORWARD(Pointer)(ptr)).*BOOST_HOF_FORWARD(PMF)(pmf))(BOOST_HOF_FORWARD(Args)(args)...)); template constexpr auto operator()(T Base::*pmf, const std::reference_wrapper& ref, Args&&... args) const BOOST_HOF_RETURNS((ref.get().*pmf)(BOOST_HOF_FORWARD(Args)(args)...)); #endif template constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(F, id_...) operator()(F&& f, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS ( f(BOOST_HOF_FORWARD(Ts)(xs)...) ); }; } BOOST_HOF_DECLARE_STATIC_VAR(apply, detail::apply_f); }} // namespace boost::hof #ifdef _MSC_VER #pragma warning(pop) #endif #endif