/*============================================================================= Copyright (c) 2015 Paul Fultz II apply_eval.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_EVAL_H #define FIT_GUARD_APPLY_EVAL_H /// apply_eval /// ========== /// /// Description /// ----------- /// /// The `apply_eval` function work like [`apply`](apply.md), except it calls /// [`eval`](eval.md) on each of its arguments. Each [`eval`](eval.md) call is /// always ordered from left-to-right. /// /// Synopsis /// -------- /// /// template /// constexpr auto apply_eval(F&& f, Ts&&... xs); /// /// Semantics /// --------- /// /// assert(apply_eval(f)(xs...) == f(eval(xs)...)); /// /// Requirements /// ------------ /// /// F must be: /// /// * [Callable](concepts.md#callable) /// /// Ts must be: /// /// * [EvaluatableFunctionObject](concepts.md#evaluatablefunctionobject) /// /// Example /// ------- /// /// struct sum_f /// { /// template /// T operator()(T x, U y) const /// { /// return x+y; /// } /// }; /// assert(fit::apply_eval(sum_f(), []{ return 1; }, []{ return 2; }) == 3); /// #include #include #include #include #include #ifndef FIT_NO_ORDERED_BRACE_INIT #if (defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 9) || defined(_MSC_VER) #define FIT_NO_ORDERED_BRACE_INIT 1 #else #define FIT_NO_ORDERED_BRACE_INIT 0 #endif #endif #if FIT_NO_ORDERED_BRACE_INIT #include #include #endif namespace fit { namespace detail { #if FIT_NO_ORDERED_BRACE_INIT template constexpr R eval_ordered(const F& f, Pack&& p) { return p(f); } template constexpr R eval_ordered(const F& f, Pack&& p, T&& x, Ts&&... xs) { return eval_ordered(f, pack_join(FIT_FORWARD(Pack)(p), fit::pack_forward(fit::eval(x))), FIT_FORWARD(Ts)(xs)...); } #else template struct eval_helper { R result; template constexpr eval_helper(const F& f, Ts&&... xs) : result(apply(f, FIT_FORWARD(Ts)(xs)...)) {} constexpr R get_result() { return (R&&)result; } }; template<> struct eval_helper { int x; template constexpr eval_helper(const F& f, Ts&&... xs) : x(apply(f, FIT_FORWARD(Ts)(xs)...), 0) {} }; #endif struct apply_eval_f { template(), fit::eval(std::declval())...) ), class=typename std::enable_if<(!std::is_void::value)>::type > constexpr R operator()(const F& f, Ts&&... xs) const { return #if FIT_NO_ORDERED_BRACE_INIT eval_ordered (f, pack(), FIT_FORWARD(Ts)(xs)...); #else eval_helper {f, fit::eval(FIT_FORWARD(Ts)(xs))...}.get_result(); #endif } template(), fit::eval(std::declval())...) ), class=typename std::enable_if<(std::is_void::value)>::type > constexpr typename detail::holder::type operator()(const F& f, Ts&&... xs) const { return (typename detail::holder::type) #if FIT_NO_ORDERED_BRACE_INIT eval_ordered (f, pack(), FIT_FORWARD(Ts)(xs)...); #else eval_helper {f, fit::eval(FIT_FORWARD(Ts)(xs))...}; #endif } }; } FIT_DECLARE_STATIC_VAR(apply_eval, detail::apply_eval_f); } // namespace fit #endif