]>
Commit | Line | Data |
---|---|---|
1 | /*! | |
2 | @file | |
3 | Forward declares `boost::hana::value`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
6 | Distributed under the Boost Software License, Version 1.0. | |
7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_FWD_VALUE_HPP | |
11 | #define BOOST_HANA_FWD_VALUE_HPP | |
12 | ||
13 | #include <boost/hana/config.hpp> | |
14 | #include <boost/hana/core/when.hpp> | |
15 | ||
16 | ||
17 | BOOST_HANA_NAMESPACE_BEGIN | |
18 | //! Return the compile-time value associated to a constant. | |
19 | //! @ingroup group-Constant | |
20 | //! | |
21 | //! This function returns the value associated to a `Constant`. That | |
22 | //! value is always a constant expression. The normal way of using | |
23 | //! `value` on an object `c` is | |
24 | //! @code | |
25 | //! constexpr auto result = hana::value<decltype(c)>(); | |
26 | //! @endcode | |
27 | //! | |
28 | //! However, for convenience, an overload of `value` is provided so that | |
29 | //! it can be called as: | |
30 | //! @code | |
31 | //! constexpr auto result = hana::value(c); | |
32 | //! @endcode | |
33 | //! | |
34 | //! This overload works by taking a `const&` to its argument, and then | |
35 | //! forwarding to the first version of `value`. Since it does not use | |
36 | //! its argument, the result can still be a constant expression, even | |
37 | //! if the argument is not a constant expression. | |
38 | //! | |
39 | //! @note | |
40 | //! `value<T>()` is tag-dispatched as `value_impl<C>::%apply<T>()`, where | |
41 | //! `C` is the tag of `T`. | |
42 | //! | |
43 | //! @note | |
44 | //! `hana::value` is an overloaded function, not a function object. | |
45 | //! Hence, it can't be passed to higher-order algorithms. If you need | |
46 | //! an equivalent function object, use `hana::value_of` instead. | |
47 | //! | |
48 | //! | |
49 | //! Example | |
50 | //! ------- | |
51 | //! @include example/value.cpp | |
52 | #ifdef BOOST_HANA_DOXYGEN_INVOKED | |
53 | template <typename T> | |
54 | constexpr auto value = []() -> decltype(auto) { | |
55 | return tag-dispatched; | |
56 | }; | |
57 | #else | |
58 | template <typename C, typename = void> | |
59 | struct value_impl : value_impl<C, when<true>> { }; | |
60 | ||
61 | template <typename T> | |
62 | constexpr decltype(auto) value(); | |
63 | ||
64 | template <typename T> | |
65 | constexpr decltype(auto) value(T const&) | |
66 | { return hana::value<T>(); } | |
67 | #endif | |
68 | ||
69 | //! Equivalent to `value`, but can be passed to higher-order algorithms. | |
70 | //! @ingroup group-Constant | |
71 | //! | |
72 | //! This function object is equivalent to `value`, except it can be passed | |
73 | //! to higher order algorithms because it is a function object. `value` | |
74 | //! can't be passed to higher-order algorithms because it is implemented | |
75 | //! as an overloaded function. | |
76 | //! | |
77 | //! @note | |
78 | //! This function is a simple alias to `value`, and hence it is not | |
79 | //! tag-dispatched and can't be customized. | |
80 | //! | |
81 | //! | |
82 | //! Example | |
83 | //! ------- | |
84 | //! @include example/value_of.cpp | |
85 | #ifdef BOOST_HANA_DOXYGEN_INVOKED | |
86 | constexpr auto value_of = [](auto const& c) -> decltype(auto) { | |
87 | return hana::value(c); | |
88 | }; | |
89 | #else | |
90 | struct value_of_t { | |
91 | template <typename T> | |
92 | constexpr decltype(auto) operator()(T const&) const | |
93 | { return hana::value<T>(); } | |
94 | }; | |
95 | ||
96 | constexpr value_of_t value_of{}; | |
97 | #endif | |
98 | BOOST_HANA_NAMESPACE_END | |
99 | ||
100 | #endif // !BOOST_HANA_FWD_VALUE_HPP |