/*============================================================================= Copyright (c) 2015 Paul Fultz II if_.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_IF_H #define BOOST_HOF_GUARD_IF_H /// if /// == /// /// Description /// ----------- /// /// The `if_` function decorator makes the function callable if the boolean /// condition is true. The `if_c` version can be used to give a boolean /// condition directly(instead of relying on an integral constant). /// /// When `if_` is false, the function is not callable. It is a subtitution /// failure to call the function. /// /// Synopsis /// -------- /// /// template /// constexpr auto if_(IntegralConstant); /// /// template /// constexpr auto if_c(F); /// /// Requirements /// ------------ /// /// IntegralConstant must be: /// /// * IntegralConstant /// /// F must be: /// /// * [ConstInvocable](ConstInvocable) /// * MoveConstructible /// /// Example /// ------- /// /// #include /// #include /// /// struct sum_f /// { /// template /// int operator()(T x, T y) const /// { /// return boost::hof::first_of( /// boost::hof::if_(std::is_integral())(boost::hof::_ + boost::hof::_), /// boost::hof::always(0) /// )(x, y); /// } /// }; /// /// int main() { /// assert(sum_f()(1, 2) == 3); /// assert(sum_f()("", "") == 0); /// } /// /// References /// ---------- /// /// * [static_if](static_if) /// #include #include #include #include #include #include namespace boost { namespace hof { namespace detail { template struct if_depend : C {}; template struct if_adaptor : detail::callable_base { BOOST_HOF_INHERIT_CONSTRUCTOR(if_adaptor, detail::callable_base) }; template struct if_adaptor { template constexpr if_adaptor(Ts&&...) noexcept {} }; template struct make_if_f { constexpr make_if_f() noexcept {} template constexpr if_adaptor operator()(F f) const BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&) { return if_adaptor(static_cast(f)); } }; struct if_f { constexpr if_f() {} template constexpr make_if_f operator()(Cond) const noexcept { return {}; } }; } #if BOOST_HOF_HAS_VARIABLE_TEMPLATES template BOOST_HOF_STATIC_CONSTEXPR detail::make_if_f if_c = {}; #else template constexpr detail::if_adaptor if_c(F f) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&) { return detail::if_adaptor(static_cast(f)); } #endif BOOST_HOF_DECLARE_STATIC_VAR(if_, detail::if_f); }} // namespace boost::hof #endif