function.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. function.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_FUNCTION_H
  8. #define BOOST_HOF_GUARD_FUNCTION_FUNCTION_H
  9. /// BOOST_HOF_STATIC_FUNCTION
  10. /// ===================
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `BOOST_HOF_STATIC_FUNCTION` macro allows initializing a function object from a
  16. /// `constexpr` expression. It uses the best practices as outlined in
  17. /// [N4381](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html).
  18. /// This includes using `const` to avoid global state, compile-time
  19. /// initialization of the function object to avoid the [static initialization
  20. /// order fiasco](https://isocpp.org/wiki/faq/ctors#static-init-order), and an
  21. /// external address of the function object that is the same across translation
  22. /// units to avoid possible One-Definition-Rule(ODR) violations.
  23. ///
  24. /// In C++17, this achieved using the `inline` keyword. However, on older
  25. /// compilers it is initialized using a reference to a static member variable.
  26. /// The static member variable is default constructed, as such the user variable
  27. /// is always default constructed regardless of the expression.
  28. ///
  29. /// By default, all functions defined with `BOOST_HOF_STATIC_FUNCTION` use the
  30. /// [`boost::hof::reveal`](/include/boost/hof/reveal) adaptor to improve error messages.
  31. ///
  32. /// Example
  33. /// -------
  34. ///
  35. /// #include <boost/hof.hpp>
  36. /// #include <cassert>
  37. ///
  38. /// struct sum_f
  39. /// {
  40. /// template<class T, class U>
  41. /// T operator()(T x, U y) const
  42. /// {
  43. /// return x+y;
  44. /// }
  45. /// };
  46. ///
  47. /// BOOST_HOF_STATIC_FUNCTION(sum) = sum_f();
  48. /// BOOST_HOF_STATIC_FUNCTION(partial_sum) = boost::hof::partial(sum_f());
  49. ///
  50. /// int main() {
  51. /// assert(sum(1, 2) == partial_sum(1)(2));
  52. /// }
  53. ///
  54. #include <boost/hof/reveal.hpp>
  55. #if !BOOST_HOF_HAS_INLINE_VARIABLES
  56. #include <boost/hof/detail/static_const_var.hpp>
  57. #include <boost/hof/detail/constexpr_deduce.hpp>
  58. #endif
  59. namespace boost { namespace hof {
  60. namespace detail {
  61. struct reveal_static_const_factory
  62. {
  63. constexpr reveal_static_const_factory()
  64. {}
  65. template<class F>
  66. constexpr reveal_adaptor<F> operator=(const F& f) const
  67. {
  68. #if BOOST_HOF_HAS_INLINE_VARIABLES
  69. #else
  70. static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(F), "Static functions must be default constructible");
  71. #endif
  72. return reveal_adaptor<F>(f);
  73. }
  74. };
  75. }}} // namespace boost::hof
  76. #if BOOST_HOF_HAS_INLINE_VARIABLES
  77. #define BOOST_HOF_STATIC_FUNCTION(name) inline const constexpr auto name = boost::hof::detail::reveal_static_const_factory()
  78. #else
  79. #define BOOST_HOF_STATIC_FUNCTION(name) BOOST_HOF_STATIC_CONST_VAR(name) = BOOST_HOF_DETAIL_MSVC_CONSTEXPR_DEDUCE boost::hof::detail::reveal_static_const_factory()
  80. #endif
  81. #endif