/*============================================================================= 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 very useful when overloading functions using template /// constraints(such as with `enable_if`). /// /// Note: This is different than the `match` 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: /// /// FunctionObject /// MoveConstructible /// /// Example /// ------- /// /// struct for_ints /// { /// void operator()(int) const /// { /// printf("Int\n"); /// } /// }; /// /// struct for_floats /// { /// void operator()(int) 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 #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(f1)), F2(fit::forward(f2)) {} template struct select : std::conditional < is_callable::value, F1, F2 > {}; template constexpr const typename select::type& select_function() const { return *this; } FIT_RETURNS_CLASS(conditional_kernel); template constexpr auto operator()(Ts && ... x) const FIT_RETURNS(FIT_CONST_THIS->select_function()(fit::forward(x)...)); }; } template struct conditional_adaptor : detail::conditional_kernel { 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(f1), kernel_base(fit::forward(fs)...)) {} struct failure : failure_for {}; }; template struct conditional_adaptor : F { FIT_INHERIT_CONSTRUCTOR(conditional_adaptor, F); struct failure : failure_for {}; }; FIT_DECLARE_STATIC_VAR(conditional, detail::make); } #endif