value.hpp 3.1 KB

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