always.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. always.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_ALWAYS_H
  8. #define BOOST_HOF_GUARD_FUNCTION_ALWAYS_H
  9. #include <boost/hof/detail/delegate.hpp>
  10. #include <boost/hof/detail/unwrap.hpp>
  11. #include <boost/hof/detail/static_const_var.hpp>
  12. /// always
  13. /// ======
  14. ///
  15. /// Description
  16. /// -----------
  17. ///
  18. /// The `always` function returns a function object that will always return
  19. /// the value given to it, no matter what parameters are passed to the
  20. /// function object. The nullary version(i.e. `always(void)`) will return
  21. /// `void`. On compilers, that don't support constexpr functions returning
  22. /// `void`, a private empty type is returned instead. This return type is
  23. /// specified as `BOOST_HOF_ALWAYS_VOID_RETURN`.
  24. ///
  25. /// Synopsis
  26. /// --------
  27. ///
  28. /// template<class T>
  29. /// constexpr auto always(T value);
  30. ///
  31. /// template<class T>
  32. /// constexpr auto always(void);
  33. ///
  34. ///
  35. /// Semantics
  36. /// ---------
  37. ///
  38. /// assert(always(x)(xs...) == x);
  39. ///
  40. /// Requirements
  41. /// ------------
  42. ///
  43. /// T must be:
  44. ///
  45. /// * CopyConstructible
  46. ///
  47. /// Example
  48. /// -------
  49. ///
  50. /// #include <boost/hof.hpp>
  51. /// #include <algorithm>
  52. /// #include <cassert>
  53. /// using namespace boost::hof;
  54. ///
  55. /// int main() {
  56. /// int ten = 10;
  57. /// assert( always(ten)(1,2,3,4,5) == 10 );
  58. /// }
  59. ///
  60. /// // Count all
  61. /// template<class Iterator, class T>
  62. /// auto count(Iterator first, Iterator last)
  63. /// {
  64. /// return std::count_if(first, last, always(true));
  65. /// }
  66. ///
  67. #ifndef BOOST_HOF_NO_CONSTEXPR_VOID
  68. #if defined(__clang__) && BOOST_HOF_HAS_RELAXED_CONSTEXPR
  69. #define BOOST_HOF_NO_CONSTEXPR_VOID 0
  70. #else
  71. #define BOOST_HOF_NO_CONSTEXPR_VOID 1
  72. #endif
  73. #endif
  74. namespace boost { namespace hof { namespace always_detail {
  75. template<class T, class=void>
  76. struct always_base
  77. {
  78. T x;
  79. BOOST_HOF_DELEGATE_CONSTRUCTOR(always_base, T, x)
  80. typedef typename detail::unwrap_reference<T>::type result_type;
  81. template<class... As>
  82. constexpr result_type
  83. operator()(As&&...) const
  84. noexcept(std::is_reference<result_type>::value || BOOST_HOF_IS_NOTHROW_COPY_CONSTRUCTIBLE(result_type))
  85. {
  86. return this->x;
  87. }
  88. };
  89. template<class T>
  90. struct always_base<T, typename std::enable_if<!BOOST_HOF_IS_EMPTY(T)>::type>
  91. {
  92. T x;
  93. constexpr always_base(T xp) noexcept(BOOST_HOF_IS_NOTHROW_COPY_CONSTRUCTIBLE(T))
  94. : x(xp)
  95. {}
  96. typedef typename detail::unwrap_reference<T>::type result_type;
  97. template<class... As>
  98. constexpr result_type
  99. operator()(As&&...) const
  100. noexcept(std::is_reference<result_type>::value || BOOST_HOF_IS_NOTHROW_COPY_CONSTRUCTIBLE(result_type))
  101. {
  102. return this->x;
  103. }
  104. };
  105. #if BOOST_HOF_NO_CONSTEXPR_VOID
  106. #define BOOST_HOF_ALWAYS_VOID_RETURN boost::hof::always_detail::always_base<void>::void_
  107. #else
  108. #define BOOST_HOF_ALWAYS_VOID_RETURN void
  109. #endif
  110. template<>
  111. struct always_base<void>
  112. {
  113. constexpr always_base() noexcept
  114. {}
  115. struct void_ {};
  116. template<class... As>
  117. constexpr BOOST_HOF_ALWAYS_VOID_RETURN
  118. operator()(As&&...) const noexcept
  119. {
  120. #if BOOST_HOF_NO_CONSTEXPR_VOID
  121. return void_();
  122. #endif
  123. }
  124. };
  125. struct always_f
  126. {
  127. template<class T>
  128. constexpr always_detail::always_base<T> operator()(T x) const noexcept(BOOST_HOF_IS_NOTHROW_COPY_CONSTRUCTIBLE(T))
  129. {
  130. return always_detail::always_base<T>(x);
  131. }
  132. constexpr always_detail::always_base<void> operator()() const noexcept
  133. {
  134. return always_detail::always_base<void>();
  135. }
  136. };
  137. struct always_ref_f
  138. {
  139. template<class T>
  140. constexpr always_detail::always_base<T&> operator()(T& x) const noexcept
  141. {
  142. return always_detail::always_base<T&>(x);
  143. }
  144. };
  145. }
  146. BOOST_HOF_DECLARE_STATIC_VAR(always, always_detail::always_f);
  147. BOOST_HOF_DECLARE_STATIC_VAR(always_ref, always_detail::always_ref_f);
  148. }} // namespace boost::hof
  149. #endif