lambda.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. lambda.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_LAMBDA_H
  8. #define BOOST_HOF_GUARD_FUNCTION_LAMBDA_H
  9. /// BOOST_HOF_STATIC_LAMBDA
  10. /// =================
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `BOOST_HOF_STATIC_LAMBDA` macro allows initializing non-capturing lambdas at
  16. /// compile-time in a `constexpr` expression.
  17. ///
  18. /// Example
  19. /// -------
  20. ///
  21. /// #include <boost/hof.hpp>
  22. /// #include <cassert>
  23. ///
  24. /// const constexpr auto add_one = BOOST_HOF_STATIC_LAMBDA(int x)
  25. /// {
  26. /// return x + 1;
  27. /// };
  28. ///
  29. /// int main() {
  30. /// assert(3 == add_one(2));
  31. /// }
  32. ///
  33. /// BOOST_HOF_STATIC_LAMBDA_FUNCTION
  34. /// ==========================
  35. ///
  36. /// Description
  37. /// -----------
  38. ///
  39. /// The `BOOST_HOF_STATIC_LAMBDA_FUNCTION` macro allows initializing a global
  40. /// function object that contains non-capturing lambdas. It also ensures that
  41. /// the global function object has a unique address across translation units.
  42. /// This helps prevent possible ODR-violations.
  43. ///
  44. /// By default, all functions defined with `BOOST_HOF_STATIC_LAMBDA_FUNCTION` use
  45. /// the `boost::hof::reveal` adaptor to improve error messages.
  46. ///
  47. /// Example
  48. /// -------
  49. ///
  50. /// #include <boost/hof.hpp>
  51. /// #include <cassert>
  52. ///
  53. /// BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one) = [](int x)
  54. /// {
  55. /// return x + 1;
  56. /// };
  57. /// int main() {
  58. /// assert(3 == add_one(2));
  59. /// }
  60. ///
  61. #include <boost/hof/config.hpp>
  62. // TODO: Move this to a detail header
  63. #if !BOOST_HOF_HAS_CONSTEXPR_LAMBDA || !BOOST_HOF_HAS_INLINE_LAMBDAS
  64. #include <type_traits>
  65. #include <utility>
  66. #include <boost/hof/detail/result_of.hpp>
  67. #include <boost/hof/reveal.hpp>
  68. #include <boost/hof/detail/constexpr_deduce.hpp>
  69. #include <boost/hof/function.hpp>
  70. #ifndef BOOST_HOF_REWRITE_STATIC_LAMBDA
  71. #ifdef _MSC_VER
  72. #define BOOST_HOF_REWRITE_STATIC_LAMBDA 1
  73. #else
  74. #define BOOST_HOF_REWRITE_STATIC_LAMBDA 0
  75. #endif
  76. #endif
  77. namespace boost { namespace hof {
  78. namespace detail {
  79. template<class F>
  80. struct static_function_wrapper
  81. {
  82. // Default constructor necessary for MSVC
  83. constexpr static_function_wrapper()
  84. {}
  85. static_assert(BOOST_HOF_IS_EMPTY(F), "Function or lambda expression must be empty");
  86. struct failure
  87. : failure_for<F>
  88. {};
  89. template<class... Ts>
  90. const F& base_function(Ts&&...) const
  91. {
  92. return reinterpret_cast<const F&>(*this);
  93. }
  94. BOOST_HOF_RETURNS_CLASS(static_function_wrapper);
  95. template<class... Ts>
  96. BOOST_HOF_SFINAE_RESULT(const F&, id_<Ts>...)
  97. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  98. (
  99. BOOST_HOF_RETURNS_REINTERPRET_CAST(const F&)(*BOOST_HOF_CONST_THIS)(BOOST_HOF_FORWARD(Ts)(xs)...)
  100. );
  101. };
  102. struct static_function_wrapper_factor
  103. {
  104. constexpr static_function_wrapper_factor()
  105. {}
  106. template<class F>
  107. constexpr static_function_wrapper<F> operator= (const F&) const
  108. {
  109. // static_assert(std::is_literal_type<static_function_wrapper<F>>::value, "Function wrapper not a literal type");
  110. return {};
  111. }
  112. };
  113. #if BOOST_HOF_REWRITE_STATIC_LAMBDA
  114. template<class T, class=void>
  115. struct is_rewritable
  116. : std::false_type
  117. {};
  118. template<class T>
  119. struct is_rewritable<T, typename detail::holder<
  120. typename T::fit_rewritable_tag
  121. >::type>
  122. : std::is_same<typename T::fit_rewritable_tag, T>
  123. {};
  124. template<class T, class=void>
  125. struct is_rewritable1
  126. : std::false_type
  127. {};
  128. template<class T>
  129. struct is_rewritable1<T, typename detail::holder<
  130. typename T::fit_rewritable1_tag
  131. >::type>
  132. : std::is_same<typename T::fit_rewritable1_tag, T>
  133. {};
  134. template<class T, class=void>
  135. struct rewrite_lambda;
  136. template<template<class...> class Adaptor, class... Ts>
  137. struct rewrite_lambda<Adaptor<Ts...>, typename std::enable_if<
  138. is_rewritable<Adaptor<Ts...>>::value
  139. >::type>
  140. {
  141. typedef Adaptor<typename rewrite_lambda<Ts>::type...> type;
  142. };
  143. template<template<class...> class Adaptor, class T, class... Ts>
  144. struct rewrite_lambda<Adaptor<T, Ts...>, typename std::enable_if<
  145. is_rewritable1<Adaptor<T, Ts...>>::value
  146. >::type>
  147. {
  148. typedef Adaptor<typename rewrite_lambda<T>::type, Ts...> type;
  149. };
  150. template<class T>
  151. struct rewrite_lambda<T, typename std::enable_if<
  152. std::is_empty<T>::value &&
  153. !is_rewritable<T>::value &&
  154. !is_rewritable1<T>::value
  155. >::type>
  156. {
  157. typedef static_function_wrapper<T> type;
  158. };
  159. template<class T>
  160. struct rewrite_lambda<T, typename std::enable_if<
  161. !std::is_empty<T>::value &&
  162. !is_rewritable<T>::value &&
  163. !is_rewritable1<T>::value
  164. >::type>
  165. {
  166. typedef T type;
  167. };
  168. #endif
  169. template<class T>
  170. struct reveal_static_lambda_function_wrapper_factor
  171. {
  172. constexpr reveal_static_lambda_function_wrapper_factor()
  173. {}
  174. #if BOOST_HOF_REWRITE_STATIC_LAMBDA
  175. template<class F>
  176. constexpr reveal_adaptor<typename rewrite_lambda<F>::type>
  177. operator=(const F&) const
  178. {
  179. return reveal_adaptor<typename rewrite_lambda<F>::type>();
  180. }
  181. #elif BOOST_HOF_HAS_CONST_FOLD
  182. template<class F>
  183. constexpr const reveal_adaptor<F>& operator=(const F&) const
  184. {
  185. return reinterpret_cast<const reveal_adaptor<F>&>(static_const_var<T>());
  186. }
  187. #else
  188. template<class F>
  189. constexpr reveal_adaptor<static_function_wrapper<F>> operator=(const F&) const
  190. {
  191. return {};
  192. }
  193. #endif
  194. };
  195. }}} // namespace boost::hof
  196. #endif
  197. #if BOOST_HOF_HAS_CONSTEXPR_LAMBDA
  198. #define BOOST_HOF_STATIC_LAMBDA []
  199. #else
  200. #define BOOST_HOF_DETAIL_MAKE_STATIC BOOST_HOF_DETAIL_CONSTEXPR_DEDUCE boost::hof::detail::static_function_wrapper_factor()
  201. #define BOOST_HOF_STATIC_LAMBDA BOOST_HOF_DETAIL_MAKE_STATIC = []
  202. #endif
  203. #if BOOST_HOF_HAS_INLINE_LAMBDAS
  204. #define BOOST_HOF_STATIC_LAMBDA_FUNCTION BOOST_HOF_STATIC_FUNCTION
  205. #else
  206. #define BOOST_HOF_DETAIL_MAKE_REVEAL_STATIC(T) BOOST_HOF_DETAIL_CONSTEXPR_DEDUCE_UNIQUE(T) boost::hof::detail::reveal_static_lambda_function_wrapper_factor<T>()
  207. #define BOOST_HOF_STATIC_LAMBDA_FUNCTION(name) \
  208. struct fit_private_static_function_ ## name {}; \
  209. BOOST_HOF_STATIC_AUTO_REF name = BOOST_HOF_DETAIL_MAKE_REVEAL_STATIC(fit_private_static_function_ ## name)
  210. #endif
  211. #endif