eval.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. eval.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_EVAL_H
  8. #define BOOST_HOF_GUARD_EVAL_H
  9. /// eval
  10. /// ====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `eval` function will evaluate a "thunk". This can be either a nullary
  16. /// function or it can be a unary function that takes the identity function as
  17. /// the first parameter(which is helpful to delay compile-time checking).
  18. /// Also, additional parameters can be passed to `eval` to delay
  19. /// compiliation(so that result can depend on template parameters).
  20. ///
  21. /// Synopsis
  22. /// --------
  23. ///
  24. /// template<class F, class... Ts>
  25. /// constexpr auto eval(F&& f, Ts&&...);
  26. ///
  27. /// Requirements
  28. /// ------------
  29. ///
  30. /// F must be:
  31. ///
  32. /// * [EvaluatableFunctionObject](EvaluatableFunctionObject)
  33. ///
  34. /// Example
  35. /// -------
  36. ///
  37. /// #include <boost/hof.hpp>
  38. /// #include <cassert>
  39. ///
  40. /// int main() {
  41. /// assert(boost::hof::eval([]{ return 3; }) == 3);
  42. /// }
  43. ///
  44. /// References
  45. /// ----------
  46. ///
  47. /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
  48. /// Proposal for C++ generic overload function
  49. /// * [static_if](static_if)
  50. /// * [Ordering evaluation of arguments](<Ordering evaluation of arguments>)
  51. ///
  52. #include <boost/hof/always.hpp>
  53. #include <boost/hof/identity.hpp>
  54. #include <boost/hof/first_of.hpp>
  55. #include <boost/hof/detail/result_of.hpp>
  56. namespace boost { namespace hof {
  57. namespace detail {
  58. struct simple_eval
  59. {
  60. template<class F, class... Ts>
  61. constexpr BOOST_HOF_SFINAE_RESULT(F)
  62. operator()(F&& f, Ts&&...xs) const BOOST_HOF_SFINAE_RETURNS
  63. (boost::hof::always_ref(f)(xs...)());
  64. };
  65. struct id_eval
  66. {
  67. template<class F, class... Ts>
  68. constexpr BOOST_HOF_SFINAE_RESULT(F, id_<decltype(boost::hof::identity)>)
  69. operator()(F&& f, Ts&&...xs) const BOOST_HOF_SFINAE_RETURNS
  70. (boost::hof::always_ref(f)(xs...)(boost::hof::identity));
  71. };
  72. }
  73. BOOST_HOF_DECLARE_STATIC_VAR(eval, boost::hof::first_of_adaptor<detail::simple_eval, detail::id_eval>);
  74. }} // namespace boost::hof
  75. #endif