/*============================================================================= 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 FIT_GUARD_APPLY_H #define FIT_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...)); /// /// Requirements /// ------------ /// /// F must be: /// /// * [Callable](concepts.md#callable) /// /// Example /// ------- /// /// struct sum_f /// { /// template /// T operator()(T x, U y) const /// { /// return x+y; /// } /// }; /// assert(fit::apply(sum_f(), 1, 2) == 3); /// #include #include #include namespace fit { namespace detail { #if FIT_HAS_MANUAL_DEDUCTION || FIT_NO_EXPRESSION_SFINAE struct apply_mem_fn { template struct convertible_args; template struct is_convertible_args; template struct is_convertible_args, convertible_args> : and_...> {}; #define FIT_APPLY_MEM_FN_CALL(cv) \ template ::type>, \ is_convertible_args, convertible_args> \ >::value>::type> \ constexpr R operator()(R (Base::*mf)(Ts...) cv, Derived&& ref, Us &&... xs) const \ { \ return (FIT_FORWARD(Derived)(ref).*mf)(FIT_FORWARD(Us)(xs)...); \ } FIT_APPLY_MEM_FN_CALL() FIT_APPLY_MEM_FN_CALL(const) FIT_APPLY_MEM_FN_CALL(volatile) FIT_APPLY_MEM_FN_CALL(const volatile) }; struct apply_mem_data { template ::type>::value )>::type> constexpr R operator()(R Base::*pmd, Derived&& ref) const { return FIT_FORWARD(Derived)(ref).*pmd; } }; template())> struct apply_deref { typedef U type; }; #endif struct apply_f { #if FIT_HAS_MANUAL_DEDUCTION || FIT_NO_EXPRESSION_SFINAE template::type>::value )>::type> constexpr FIT_SFINAE_MANUAL_RESULT(apply_mem_fn, id_, id_, id_...) operator()(F&& f, T&& obj, Ts&&... xs) const FIT_SFINAE_MANUAL_RETURNS ( apply_mem_fn()(f, FIT_FORWARD(T)(obj), FIT_FORWARD(Ts)(xs)...) ); template::type, class=typename std::enable_if<( std::is_member_function_pointer::type>::value )>::type> constexpr FIT_SFINAE_MANUAL_RESULT(apply_mem_fn, id_, id_, id_...) operator()(F&& f, T&& obj, Ts&&... xs) const FIT_SFINAE_MANUAL_RETURNS ( apply_mem_fn()(f, *FIT_FORWARD(T)(obj), FIT_FORWARD(Ts)(xs)...) ); template::type>::value )>::type> constexpr FIT_SFINAE_MANUAL_RESULT(apply_mem_data, id_, id_) operator()(F&& f, T&& obj) const FIT_SFINAE_MANUAL_RETURNS ( apply_mem_data()(f, FIT_FORWARD(T)(obj)) ); template::type, class=typename std::enable_if<( std::is_member_object_pointer::type>::value )>::type> constexpr FIT_SFINAE_MANUAL_RESULT(apply_mem_data, id_, id_) operator()(F&& f, T&& obj) const FIT_SFINAE_MANUAL_RETURNS ( apply_mem_data()(f, *FIT_FORWARD(T)(obj)) ); #else template constexpr auto operator()(T Base::*pmd, Derived&& ref) const FIT_RETURNS(FIT_FORWARD(Derived)(ref).*pmd); template constexpr auto operator()(PMD&& pmd, Pointer&& ptr) const FIT_RETURNS((*FIT_FORWARD(Pointer)(ptr)).*FIT_FORWARD(PMD)(pmd)); template constexpr auto operator()(T Base::*pmf, Derived&& ref, Args&&... args) const FIT_RETURNS((FIT_FORWARD(Derived)(ref).*pmf)(FIT_FORWARD(Args)(args)...)); template constexpr auto operator()(PMF&& pmf, Pointer&& ptr, Args&&... args) const FIT_RETURNS(((*FIT_FORWARD(Pointer)(ptr)).*FIT_FORWARD(PMF)(pmf))(FIT_FORWARD(Args)(args)...)); #endif template constexpr FIT_SFINAE_RESULT(F, id_...) operator()(F&& f, Ts&&... xs) const FIT_SFINAE_RETURNS ( f(FIT_FORWARD(Ts)(xs)...) ); }; } FIT_DECLARE_STATIC_VAR(apply, detail::apply_f); } // namespace fit #endif