lambda.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /// Note: due to compiler limitations, a global function declared with
  48. /// `BOOST_HOF_STATIC_LAMBDA_FUNCTION` is not guaranteed to have a unique
  49. /// address across translation units when compiled with pre-C++17 MSVC.
  50. ///
  51. /// Example
  52. /// -------
  53. ///
  54. /// #include <boost/hof.hpp>
  55. /// #include <cassert>
  56. ///
  57. /// BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one) = [](int x)
  58. /// {
  59. /// return x + 1;
  60. /// };
  61. /// int main() {
  62. /// assert(3 == add_one(2));
  63. /// }
  64. ///
  65. #include <boost/hof/config.hpp>
  66. // TODO: Move this to a detail header
  67. #if !BOOST_HOF_HAS_CONSTEXPR_LAMBDA || !BOOST_HOF_HAS_INLINE_LAMBDAS
  68. #include <type_traits>
  69. #include <utility>
  70. #include <boost/hof/detail/result_of.hpp>
  71. #include <boost/hof/reveal.hpp>
  72. #include <boost/hof/detail/constexpr_deduce.hpp>
  73. #include <boost/hof/function.hpp>
  74. #ifndef BOOST_HOF_REWRITE_STATIC_LAMBDA
  75. #ifdef _MSC_VER
  76. #define BOOST_HOF_REWRITE_STATIC_LAMBDA 1
  77. #else
  78. #define BOOST_HOF_REWRITE_STATIC_LAMBDA 0
  79. #endif
  80. #endif
  81. namespace boost { namespace hof {
  82. namespace detail {
  83. template<class F>
  84. struct static_function_wrapper
  85. {
  86. // Default constructor necessary for MSVC
  87. constexpr static_function_wrapper()
  88. {}
  89. static_assert(BOOST_HOF_IS_EMPTY(F), "Function or lambda expression must be empty");
  90. struct failure
  91. : failure_for<F>
  92. {};
  93. template<class... Ts>
  94. const F& base_function(Ts&&...) const
  95. {
  96. return reinterpret_cast<const F&>(*this);
  97. }
  98. BOOST_HOF_RETURNS_CLASS(static_function_wrapper);
  99. template<class... Ts>
  100. BOOST_HOF_SFINAE_RESULT(const F&, id_<Ts>...)
  101. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  102. (
  103. BOOST_HOF_RETURNS_REINTERPRET_CAST(const F&)(*BOOST_HOF_CONST_THIS)(BOOST_HOF_FORWARD(Ts)(xs)...)
  104. );
  105. };
  106. struct static_function_wrapper_factor
  107. {
  108. constexpr static_function_wrapper_factor()
  109. {}
  110. template<class F>
  111. constexpr static_function_wrapper<F> operator= (const F&) const
  112. {
  113. // static_assert(std::is_literal_type<static_function_wrapper<F>>::value, "Function wrapper not a literal type");
  114. return {};
  115. }
  116. };
  117. #if BOOST_HOF_REWRITE_STATIC_LAMBDA
  118. template<class T, class=void>
  119. struct is_rewritable
  120. : std::false_type
  121. {};
  122. template<class T>
  123. struct is_rewritable<T, typename detail::holder<
  124. typename T::fit_rewritable_tag
  125. >::type>
  126. : std::is_same<typename T::fit_rewritable_tag, T>
  127. {};
  128. template<class T, class=void>
  129. struct is_rewritable1
  130. : std::false_type
  131. {};
  132. template<class T>
  133. struct is_rewritable1<T, typename detail::holder<
  134. typename T::fit_rewritable1_tag
  135. >::type>
  136. : std::is_same<typename T::fit_rewritable1_tag, T>
  137. {};
  138. template<class T, class=void>
  139. struct rewrite_lambda;
  140. template<template<class...> class Adaptor, class... Ts>
  141. struct rewrite_lambda<Adaptor<Ts...>, typename std::enable_if<
  142. is_rewritable<Adaptor<Ts...>>::value
  143. >::type>
  144. {
  145. typedef Adaptor<typename rewrite_lambda<Ts>::type...> type;
  146. };
  147. template<template<class...> class Adaptor, class T, class... Ts>
  148. struct rewrite_lambda<Adaptor<T, Ts...>, typename std::enable_if<
  149. is_rewritable1<Adaptor<T, Ts...>>::value
  150. >::type>
  151. {
  152. typedef Adaptor<typename rewrite_lambda<T>::type, Ts...> type;
  153. };
  154. template<class T>
  155. struct rewrite_lambda<T, typename std::enable_if<
  156. std::is_empty<T>::value &&
  157. !is_rewritable<T>::value &&
  158. !is_rewritable1<T>::value
  159. >::type>
  160. {
  161. typedef static_function_wrapper<T> type;
  162. };
  163. template<class T>
  164. struct rewrite_lambda<T, typename std::enable_if<
  165. !std::is_empty<T>::value &&
  166. !is_rewritable<T>::value &&
  167. !is_rewritable1<T>::value
  168. >::type>
  169. {
  170. typedef T type;
  171. };
  172. #endif
  173. template<class T>
  174. struct reveal_static_lambda_function_wrapper_factor
  175. {
  176. constexpr reveal_static_lambda_function_wrapper_factor()
  177. {}
  178. #if BOOST_HOF_REWRITE_STATIC_LAMBDA
  179. template<class F>
  180. constexpr reveal_adaptor<typename rewrite_lambda<F>::type>
  181. operator=(const F&) const
  182. {
  183. return reveal_adaptor<typename rewrite_lambda<F>::type>();
  184. }
  185. #elif BOOST_HOF_HAS_CONST_FOLD
  186. template<class F>
  187. constexpr const reveal_adaptor<F>& operator=(const F&) const
  188. {
  189. return reinterpret_cast<const reveal_adaptor<F>&>(static_const_var<T>());
  190. }
  191. #else
  192. template<class F>
  193. constexpr reveal_adaptor<static_function_wrapper<F>> operator=(const F&) const
  194. {
  195. return {};
  196. }
  197. #endif
  198. };
  199. }}} // namespace boost::hof
  200. #endif
  201. #if BOOST_HOF_HAS_CONSTEXPR_LAMBDA
  202. #define BOOST_HOF_STATIC_LAMBDA []
  203. #else
  204. #define BOOST_HOF_DETAIL_MAKE_STATIC BOOST_HOF_DETAIL_CONSTEXPR_DEDUCE boost::hof::detail::static_function_wrapper_factor()
  205. #define BOOST_HOF_STATIC_LAMBDA BOOST_HOF_DETAIL_MAKE_STATIC = []
  206. #endif
  207. #if BOOST_HOF_HAS_INLINE_LAMBDAS
  208. #define BOOST_HOF_STATIC_LAMBDA_FUNCTION BOOST_HOF_STATIC_FUNCTION
  209. #else
  210. #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>()
  211. #define BOOST_HOF_STATIC_LAMBDA_FUNCTION(name) \
  212. struct fit_private_static_function_ ## name {}; \
  213. BOOST_HOF_STATIC_AUTO_REF name = BOOST_HOF_DETAIL_MAKE_REVEAL_STATIC(fit_private_static_function_ ## name)
  214. #endif
  215. #endif