/*============================================================================= 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 lambdas at compile-time /// in a `constexpr` expression. /// /// Example /// ------- /// /// const constexpr auto add_one = FIT_STATIC_LAMBDA(int x) /// { /// return x + 1; /// }; /// #include #include #include namespace fit { namespace detail { template struct lambda_wrapper { static_assert(std::is_empty::value, "Lambdas must be empty"); FIT_RETURNS_CLASS(lambda_wrapper); template auto operator()(Ts&&... xs) const FIT_RETURNS ( FIT_RETURNS_REINTERPRET_CAST(const F&)(*FIT_CONST_THIS)(fit::forward(xs)...) ); }; struct lambda_wrapper_factor { template constexpr lambda_wrapper operator += (F*) { static_assert(std::is_literal_type>::value, "Lambda wrapper not a literal type"); return {}; } }; struct lambda_addr { template friend typename std::remove_reference::type *operator+(lambda_addr, T &&t) { return &t; } }; } #define FIT_STATIC_LAMBDA fit::detail::lambda_wrapper_factor() += true ? nullptr : fit::detail::lambda_addr() + [] } #endif