/*============================================================================= Copyright (c) 2012 Paul Fultz II conditional.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_FUNCTION_CONDITIONAL_H #define FIT_GUARD_FUNCTION_CONDITIONAL_H /// conditional /// =========== /// /// Description /// ----------- /// /// The `conditional` function adaptor combines several functions together. If /// the first function can not be called, then it will try to call the next /// function. This can be very useful when overloading functions using /// template constraints(such as with `enable_if`). /// /// Note: This is different than the [`match`](match.md) function adaptor, which /// can lead to ambiguities. Instead, `conditional` will call the first function /// that is callable, regardless if there is another function that could be /// called as well. /// /// Synopsis /// -------- /// /// template /// constexpr conditional_adaptor conditional(Fs... fs); /// /// Requirements /// ------------ /// /// Fs must be: /// /// * [Callable](concepts.md#callable) /// * MoveConstructible /// /// Example /// ------- /// /// struct for_ints /// { /// void operator()(int) const /// { /// printf("Int\n"); /// } /// }; /// /// struct for_floats /// { /// void operator()(float) const /// { /// printf("Float\n"); /// } /// }; /// /// conditional(for_ints(), for_floats())(3.0); /// /// This will print `Int` because the `for_floats` function object won't ever be /// called. Due to the conversion rules in C++, the `for_ints` function can be /// called on floats, so it is chosen by `conditional` first, even though /// `for_floats` is a better match. /// /// So, the order of the functions in the `conditional_adaptor` are very important /// to how the function is chosen. #include #include #include #include #include #include #include namespace fit { namespace detail { template struct conditional_kernel : F1, F2 { FIT_INHERIT_DEFAULT(conditional_kernel, F1, F2) template constexpr conditional_kernel(A&& f1, B&& f2) : F1(FIT_FORWARD(A)(f1)), F2(FIT_FORWARD(B)(f2)) {} template::type> constexpr conditional_kernel(X&& x) : F1(FIT_FORWARD(X)(x)) {} template struct select : std::conditional < is_callable::value, F1, F2 > {}; FIT_RETURNS_CLASS(conditional_kernel); template::type> constexpr FIT_SFINAE_RESULT(typename select::type, id_...) operator()(Ts && ... x) const FIT_SFINAE_RETURNS ( FIT_RETURNS_STATIC_CAST(const F&)(*FIT_CONST_THIS)(FIT_FORWARD(Ts)(x)...) ); }; } template struct conditional_adaptor : detail::conditional_kernel { typedef conditional_adaptor fit_rewritable_tag; typedef FIT_JOIN(conditional_adaptor, Fs...) kernel_base; typedef detail::conditional_kernel base; FIT_INHERIT_DEFAULT(conditional_adaptor, base) template constexpr conditional_adaptor(X&& f1, Xs&& ... fs) : base(FIT_FORWARD(X)(f1), kernel_base(FIT_FORWARD(Xs)(fs)...)) {} template constexpr conditional_adaptor(X&& f1) : base(FIT_FORWARD(X)(f1)) {} struct failure : failure_for {}; }; template struct conditional_adaptor : F { typedef conditional_adaptor fit_rewritable_tag; FIT_INHERIT_CONSTRUCTOR(conditional_adaptor, F); struct failure : failure_for {}; }; template struct conditional_adaptor : detail::conditional_kernel { typedef detail::conditional_kernel base; typedef conditional_adaptor fit_rewritable_tag; FIT_INHERIT_CONSTRUCTOR(conditional_adaptor, base); struct failure : failure_for {}; }; FIT_DECLARE_STATIC_VAR(conditional, detail::make); } // namespace fit #endif