/*============================================================================= Copyright (c) 2014 Paul Fultz II lambda.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_LAMBDA_H #define FIT_GUARD_FUNCTION_LAMBDA_H /// FIT_STATIC_LAMBDA /// ================= /// /// Description /// ----------- /// /// The `FIT_STATIC_LAMBDA` macro allows initializing non-capturing lambdas at /// compile-time in a `constexpr` expression. /// /// Example /// ------- /// /// #include /// #include /// /// const constexpr auto add_one = FIT_STATIC_LAMBDA(int x) /// { /// return x + 1; /// }; /// /// int main() { /// assert(3 == add_one(2)); /// } /// /// FIT_STATIC_LAMBDA_FUNCTION /// ========================== /// /// Description /// ----------- /// /// The `FIT_STATIC_LAMBDA_FUNCTION` macro allows initializing a global /// function object that contains non-capturing lambdas. It also ensures that /// the global function object has a unique address across translation units. /// This helps prevent possible ODR-violations. /// /// By default, all functions defined with `FIT_STATIC_LAMBDA_FUNCTION` use /// the `fit::reveal` adaptor to improve error messages. /// /// Example /// ------- /// /// #include /// #include /// /// FIT_STATIC_LAMBDA_FUNCTION(add_one) = [](int x) /// { /// return x + 1; /// }; /// int main() { /// assert(3 == add_one(2)); /// } /// #include // TODO: Move this to a detail header #if !FIT_HAS_CONSTEXPR_LAMBDA || !FIT_HAS_INLINE_LAMBDAS #include #include #include #include #include #include #ifndef FIT_REWRITE_STATIC_LAMBDA #ifdef _MSC_VER #define FIT_REWRITE_STATIC_LAMBDA 1 #else #define FIT_REWRITE_STATIC_LAMBDA 0 #endif #endif namespace fit { namespace detail { template struct static_function_wrapper { // Default constructor necessary for MSVC constexpr static_function_wrapper() {} static_assert(FIT_IS_EMPTY(F), "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 FIT_SFINAE_RESULT(const F&, id_...) operator()(Ts&&... xs) const FIT_SFINAE_RETURNS ( FIT_RETURNS_REINTERPRET_CAST(const F&)(*FIT_CONST_THIS)(FIT_FORWARD(Ts)(xs)...) ); }; struct static_function_wrapper_factor { constexpr static_function_wrapper_factor() {} template constexpr static_function_wrapper operator= (const F&) const { // static_assert(std::is_literal_type>::value, "Function wrapper not a literal type"); return {}; } }; #if FIT_REWRITE_STATIC_LAMBDA template struct is_rewritable : std::false_type {}; template struct is_rewritable::type> : std::is_same {}; template struct is_rewritable1 : std::false_type {}; template struct is_rewritable1::type> : std::is_same {}; template struct rewrite_lambda; template class Adaptor, class... Ts> struct rewrite_lambda, typename std::enable_if< is_rewritable>::value >::type> { typedef Adaptor::type...> type; }; template class Adaptor, class T, class... Ts> struct rewrite_lambda, typename std::enable_if< is_rewritable1>::value >::type> { typedef Adaptor::type, Ts...> type; }; template struct rewrite_lambda::value && !is_rewritable::value && !is_rewritable1::value >::type> { typedef static_function_wrapper type; }; template struct rewrite_lambda::value && !is_rewritable::value && !is_rewritable1::value >::type> { typedef T type; }; #endif template struct reveal_static_lambda_function_wrapper_factor { constexpr reveal_static_lambda_function_wrapper_factor() {} #if FIT_REWRITE_STATIC_LAMBDA template constexpr reveal_adaptor::type> operator=(const F&) const { return reveal_adaptor::type>(); } #elif FIT_HAS_CONST_FOLD template constexpr const reveal_adaptor& operator=(const F&) const { return reinterpret_cast&>(static_const_var()); } #else template constexpr reveal_adaptor> operator=(const F&) const { return {}; } #endif }; }} // namespace fit #endif #if FIT_HAS_CONSTEXPR_LAMBDA #define FIT_STATIC_LAMBDA [] #else #define FIT_DETAIL_MAKE_STATIC FIT_DETAIL_CONSTEXPR_DEDUCE fit::detail::static_function_wrapper_factor() #define FIT_STATIC_LAMBDA FIT_DETAIL_MAKE_STATIC = [] #endif #if FIT_HAS_INLINE_LAMBDAS #define FIT_STATIC_LAMBDA_FUNCTION FIT_STATIC_FUNCTION #else #define FIT_DETAIL_MAKE_REVEAL_STATIC(T) FIT_DETAIL_CONSTEXPR_DEDUCE_UNIQUE(T) fit::detail::reveal_static_lambda_function_wrapper_factor() #define FIT_STATIC_LAMBDA_FUNCTION(name) \ struct fit_private_static_function_ ## name {}; \ FIT_STATIC_AUTO_REF name = FIT_DETAIL_MAKE_REVEAL_STATIC(fit_private_static_function_ ## name) #endif #endif