returns.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. returns.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_RETURNS_H
  8. #define BOOST_HOF_GUARD_RETURNS_H
  9. /// BOOST_HOF_RETURNS
  10. /// ===========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `BOOST_HOF_RETURNS` macro defines the function as the expression
  16. /// equivalence. It does this by deducing `noexcept` and the return type by
  17. /// using a trailing `decltype`. Instead of repeating the expression for the
  18. /// return type, `noexcept` clause and the function body, this macro will
  19. /// reduce the code duplication from that.
  20. ///
  21. /// Note: The expression used to deduce the return the type will also
  22. /// constrain the template function and deduce `noexcept` as well, which is
  23. /// different behaviour than using C++14's return type deduction.
  24. ///
  25. /// Synopsis
  26. /// --------
  27. ///
  28. /// #define BOOST_HOF_RETURNS(...)
  29. ///
  30. ///
  31. /// Example
  32. /// -------
  33. ///
  34. /// #include <boost/hof.hpp>
  35. /// #include <cassert>
  36. ///
  37. /// template<class T, class U>
  38. /// auto sum(T x, U y) BOOST_HOF_RETURNS(x+y);
  39. ///
  40. /// int main() {
  41. /// assert(3 == sum(1, 2));
  42. /// }
  43. ///
  44. ///
  45. /// Incomplete this
  46. /// ---------------
  47. ///
  48. /// Description
  49. /// -----------
  50. ///
  51. /// On some non-conformant compilers, such as gcc, the `this` variable cannot
  52. /// be used inside the `BOOST_HOF_RETURNS` macro because it is considered an
  53. /// incomplete type. So the following macros are provided to help workaround
  54. /// the issue.
  55. ///
  56. ///
  57. /// Synopsis
  58. /// --------
  59. ///
  60. /// // Declares the type of the `this` variable
  61. /// #define BOOST_HOF_RETURNS_CLASS(...)
  62. /// // Used to refer to the `this` variable in the BOOST_HOF_RETURNS macro
  63. /// #define BOOST_HOF_THIS
  64. /// // Used to refer to the const `this` variable in the BOOST_HOF_RETURNS macro
  65. /// #define BOOST_HOF_CONST_THIS
  66. ///
  67. ///
  68. /// Example
  69. /// -------
  70. ///
  71. /// #include <boost/hof.hpp>
  72. /// #include <cassert>
  73. ///
  74. /// struct add_1
  75. /// {
  76. /// int a;
  77. /// add_1() : a(1) {}
  78. ///
  79. /// BOOST_HOF_RETURNS_CLASS(add_1);
  80. ///
  81. /// template<class T>
  82. /// auto operator()(T x) const
  83. /// BOOST_HOF_RETURNS(x+BOOST_HOF_CONST_THIS->a);
  84. /// };
  85. ///
  86. /// int main() {
  87. /// assert(3 == add_1()(2));
  88. /// }
  89. ///
  90. ///
  91. /// Mangling overloads
  92. /// ------------------
  93. ///
  94. /// Description
  95. /// -----------
  96. ///
  97. /// On older compilers some operations done in the expressions cannot be
  98. /// properly mangled. These macros help provide workarounds for these
  99. /// operations on older compilers.
  100. ///
  101. ///
  102. /// Synopsis
  103. /// --------
  104. ///
  105. /// // Explicitly defines the type for name mangling
  106. /// #define BOOST_HOF_MANGLE_CAST(...)
  107. /// // C cast for name mangling
  108. /// #define BOOST_HOF_RETURNS_C_CAST(...)
  109. /// // Reinterpret cast for name mangling
  110. /// #define BOOST_HOF_RETURNS_REINTERPRET_CAST(...)
  111. /// // Static cast for name mangling
  112. /// #define BOOST_HOF_RETURNS_STATIC_CAST(...)
  113. /// // Construction for name mangling
  114. /// #define BOOST_HOF_RETURNS_CONSTRUCT(...)
  115. ///
  116. #include <boost/hof/config.hpp>
  117. #include <utility>
  118. #include <boost/hof/detail/forward.hpp>
  119. #include <boost/hof/detail/noexcept.hpp>
  120. #define BOOST_HOF_EAT(...)
  121. #define BOOST_HOF_REM(...) __VA_ARGS__
  122. #if BOOST_HOF_HAS_COMPLETE_DECLTYPE && BOOST_HOF_HAS_MANGLE_OVERLOAD
  123. #ifdef _MSC_VER
  124. // Since decltype can't be used in noexcept on MSVC, we can't check for noexcept
  125. // move constructors.
  126. #define BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(...) BOOST_HOF_NOEXCEPT(noexcept(__VA_ARGS__))
  127. #else
  128. #define BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(...) BOOST_HOF_NOEXCEPT(noexcept(static_cast<decltype(__VA_ARGS__)>(__VA_ARGS__)))
  129. #endif
  130. #define BOOST_HOF_RETURNS(...) \
  131. BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(__VA_ARGS__) \
  132. -> decltype(__VA_ARGS__) { return __VA_ARGS__; }
  133. #define BOOST_HOF_THIS this
  134. #define BOOST_HOF_CONST_THIS this
  135. #define BOOST_HOF_RETURNS_CLASS(...) \
  136. void fit_returns_class_check() \
  137. { \
  138. static_assert(std::is_same<__VA_ARGS__*, decltype(this)>::value, \
  139. "Returns class " #__VA_ARGS__ " type doesn't match"); \
  140. }
  141. #define BOOST_HOF_MANGLE_CAST(...) BOOST_HOF_REM
  142. #define BOOST_HOF_RETURNS_C_CAST(...) (__VA_ARGS__) BOOST_HOF_REM
  143. #define BOOST_HOF_RETURNS_REINTERPRET_CAST(...) reinterpret_cast<__VA_ARGS__>
  144. #define BOOST_HOF_RETURNS_STATIC_CAST(...) static_cast<__VA_ARGS__>
  145. #define BOOST_HOF_RETURNS_CONSTRUCT(...) __VA_ARGS__
  146. #else
  147. #include <boost/hof/detail/pp.hpp>
  148. #define BOOST_HOF_RETURNS_RETURN(...) return BOOST_HOF_RETURNS_RETURN_X(BOOST_HOF_PP_WALL(__VA_ARGS__))
  149. #define BOOST_HOF_RETURNS_RETURN_X(...) __VA_ARGS__
  150. #ifdef _MSC_VER
  151. // Since decltype can't be used in noexcept on MSVC, we can't check for noexcept
  152. // move constructors.
  153. #define BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(...) BOOST_HOF_NOEXCEPT(noexcept(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(__VA_ARGS__)))
  154. #else
  155. #define BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(...) BOOST_HOF_NOEXCEPT(noexcept(static_cast<decltype(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(__VA_ARGS__))>(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(__VA_ARGS__))))
  156. #endif
  157. #define BOOST_HOF_RETURNS_DECLTYPE(...) \
  158. -> decltype(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(__VA_ARGS__))
  159. #define BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(...) BOOST_HOF_RETURNS_DECLTYPE_CONTEXT_X(BOOST_HOF_PP_WALL(__VA_ARGS__))
  160. #define BOOST_HOF_RETURNS_DECLTYPE_CONTEXT_X(...) __VA_ARGS__
  161. #define BOOST_HOF_RETURNS_THAT(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  162. (boost::hof::detail::check_this<__VA_ARGS__, decltype(this)>(), this), \
  163. std::declval<__VA_ARGS__>() \
  164. )
  165. #define BOOST_HOF_THIS BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_THAT)(fit_this_type)
  166. #define BOOST_HOF_CONST_THIS BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_THAT)(fit_const_this_type)
  167. #define BOOST_HOF_RETURNS_CLASS(...) typedef __VA_ARGS__* fit_this_type; typedef const __VA_ARGS__* fit_const_this_type
  168. #define BOOST_HOF_RETURNS(...) \
  169. BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(__VA_ARGS__) \
  170. BOOST_HOF_RETURNS_DECLTYPE(__VA_ARGS__) \
  171. { BOOST_HOF_RETURNS_RETURN(__VA_ARGS__); }
  172. namespace boost { namespace hof { namespace detail {
  173. template<class Assumed, class T>
  174. struct check_this
  175. {
  176. static_assert(std::is_same<T, Assumed>::value, "Incorret BOOST_HOF_THIS or BOOST_HOF_CONST_THIS used.");
  177. };
  178. }}} // namespace boost::hof
  179. #endif
  180. #if BOOST_HOF_HAS_MANGLE_OVERLOAD
  181. #define BOOST_HOF_MANGLE_CAST(...) BOOST_HOF_REM
  182. #define BOOST_HOF_RETURNS_C_CAST(...) (__VA_ARGS__) BOOST_HOF_REM
  183. #define BOOST_HOF_RETURNS_REINTERPRET_CAST(...) reinterpret_cast<__VA_ARGS__>
  184. #define BOOST_HOF_RETURNS_STATIC_CAST(...) static_cast<__VA_ARGS__>
  185. #define BOOST_HOF_RETURNS_CONSTRUCT(...) __VA_ARGS__
  186. #else
  187. #define BOOST_HOF_RETURNS_DERAIL_MANGLE_CAST(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  188. BOOST_HOF_REM, \
  189. std::declval<__VA_ARGS__>() BOOST_HOF_EAT \
  190. )
  191. #define BOOST_HOF_MANGLE_CAST BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_DERAIL_MANGLE_CAST)
  192. #define BOOST_HOF_RETURNS_DERAIL_C_CAST(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  193. (__VA_ARGS__) BOOST_HOF_REM, \
  194. std::declval<__VA_ARGS__>() BOOST_HOF_EAT \
  195. )
  196. #define BOOST_HOF_RETURNS_C_CAST BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_DERAIL_C_CAST)
  197. #define BOOST_HOF_RETURNS_DERAIL_REINTERPRET_CAST(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  198. reinterpret_cast<__VA_ARGS__>, \
  199. std::declval<__VA_ARGS__>() BOOST_HOF_EAT \
  200. )
  201. #define BOOST_HOF_RETURNS_REINTERPRET_CAST BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_DERAIL_REINTERPRET_CAST)
  202. #define BOOST_HOF_RETURNS_DERAIL_STATIC_CAST(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  203. static_cast<__VA_ARGS__>, \
  204. std::declval<__VA_ARGS__>() BOOST_HOF_EAT \
  205. )
  206. #define BOOST_HOF_RETURNS_STATIC_CAST BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_DERAIL_STATIC_CAST)
  207. #define BOOST_HOF_RETURNS_DERAIL_CONSTRUCT(...) BOOST_HOF_PP_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_RETURNS_DECLTYPE_CONTEXT(())))(\
  208. __VA_ARGS__, \
  209. std::declval<__VA_ARGS__>() BOOST_HOF_EAT \
  210. )
  211. #define BOOST_HOF_RETURNS_CONSTRUCT BOOST_HOF_PP_RAIL(BOOST_HOF_RETURNS_DERAIL_CONSTRUCT)
  212. #endif
  213. #define BOOST_HOF_AUTO_FORWARD(...) BOOST_HOF_RETURNS_STATIC_CAST(decltype(__VA_ARGS__))(__VA_ARGS__)
  214. #endif