/*============================================================================= Copyright (c) 2014 Paul Fultz II function.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_FUNCTION_H #define FIT_GUARD_FUNCTION_FUNCTION_H /// FIT_STATIC_FUNCTION /// =================== /// /// Description /// ----------- /// /// The `FIT_STATIC_FUNCTION` macro allows initializing a function object from /// lambdas and adaptors at compile-time in a `constexpr` expression. /// /// Example /// ------- /// /// FIT_STATIC_FUNCTION(sum) = fit::partial([](int x, int y) /// { /// return x + y; /// }); /// #include #include #include #include #include namespace fit { namespace detail { template struct static_function_wrapper { static_assert(std::is_empty::value, "Function or lambda expression must be empty"); struct failure : failure_for {}; template const F& base_function(Ts&&...) const { return reinterpret_cast(*this); } FIT_RETURNS_CLASS(static_function_wrapper); template auto operator()(Ts&&... xs) const FIT_RETURNS ( FIT_RETURNS_REINTERPRET_CAST(const F&)(*FIT_CONST_THIS)(fit::forward(xs)...) ); }; struct static_function_wrapper_factor { template constexpr static_function_wrapper operator += (F*) { static_assert(std::is_literal_type>::value, "Function wrapper not a literal type"); return {}; } }; struct reveal_static_function_wrapper_factor { template constexpr reveal_adaptor> operator += (F*) { return {}; } }; struct static_addr { template typename std::remove_reference::type *operator=(T &&t) const { return &t; } }; }} #define FIT_DETAIL_MAKE_STATIC fit::detail::static_function_wrapper_factor() += true ? nullptr : fit::detail::static_addr() #define FIT_DETAIL_MAKE_REVEAL_STATIC fit::detail::reveal_static_function_wrapper_factor() += true ? nullptr : fit::detail::static_addr() #define FIT_STATIC_FUNCTION(name) FIT_STATIC_CONSTEXPR auto name = FIT_DETAIL_MAKE_REVEAL_STATIC #endif