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