range.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. @file
  3. Forward declares `boost::hana::range`.
  4. @copyright Louis Dionne 2013-2016
  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_RANGE_HPP
  9. #define BOOST_HANA_FWD_RANGE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/fwd/core/make.hpp>
  12. #include <boost/hana/fwd/integral_constant.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  15. //! @ingroup group-datatypes
  16. //! Compile-time half-open interval of `hana::integral_constant`s.
  17. //!
  18. //! A `range` represents a half-open interval of the form `[from, to)`
  19. //! containing `hana::integral_constant`s of a given type. The `[from, to)`
  20. //! notation represents the values starting at `from` (inclusively) up
  21. //! to but excluding `from`. In other words, it is a bit like the list
  22. //! `from, from+1, ..., to-1`.
  23. //!
  24. //! In particular, note that the bounds of the range can be any
  25. //! `hana::integral_constant`s (negative numbers are allowed) and the
  26. //! range does not have to start at zero. The only requirement is that
  27. //! `from <= to`.
  28. //!
  29. //! @note
  30. //! The representation of `hana::range` is implementation defined. In
  31. //! particular, one should not take for granted the number and types
  32. //! of template parameters. The proper way to create a `hana::range`
  33. //! is to use `hana::range_c` or `hana::make_range`.
  34. //!
  35. //!
  36. //! Modeled concepts
  37. //! ----------------
  38. //! 1. `Comparable`\n
  39. //! Two ranges are equal if and only if they are both empty or they both
  40. //! span the same interval.
  41. //! @include example/range/comparable.cpp
  42. //!
  43. //! 2. `Foldable`\n
  44. //! Folding a `range` is equivalent to folding a list of the
  45. //! `integral_constant`s in the interval it spans.
  46. //! @include example/range/foldable.cpp
  47. //!
  48. //! 3. `Iterable`\n
  49. //! Iterating over a `range` is equivalent to iterating over a list of
  50. //! the values it spans. In other words, iterating over the range
  51. //! `[from, to)` is equivalent to iterating over a list containing
  52. //! `from, from+1, from+2, ..., to-1`. Also note that `operator[]` can
  53. //! be used in place of the `at` function.
  54. //! @include example/range/iterable.cpp
  55. //!
  56. //! 4. `Searchable`\n
  57. //! Searching a `range` is equivalent to searching a list of the values
  58. //! in the range `[from, to)`, but it is much more compile-time efficient.
  59. //! @include example/range/searchable.cpp
  60. template <typename T, T from, T to>
  61. struct range {
  62. //! Equivalent to `hana::equal`
  63. template <typename X, typename Y>
  64. friend constexpr auto operator==(X&& x, Y&& y);
  65. //! Equivalent to `hana::not_equal`
  66. template <typename X, typename Y>
  67. friend constexpr auto operator!=(X&& x, Y&& y);
  68. //! Equivalent to `hana::at`
  69. template <typename N>
  70. constexpr decltype(auto) operator[](N&& n);
  71. };
  72. #else
  73. template <typename T, T from, T to>
  74. struct range;
  75. #endif
  76. //! Tag representing a `hana::range`.
  77. //! @relates hana::range
  78. struct range_tag { };
  79. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  80. //! Create a `hana::range` representing a half-open interval of
  81. //! `integral_constant`s.
  82. //! @relates hana::range
  83. //!
  84. //! Given two `IntegralConstant`s `from` and `to`, `make<range_tag>`
  85. //! returns a `hana::range` representing the half-open interval of
  86. //! `integral_constant`s `[from, to)`. `from` and `to` must form a
  87. //! valid interval, which means that `from <= to` must be true. Otherwise,
  88. //! a compilation error is triggered. Also note that if `from` and `to`
  89. //! are `IntegralConstant`s with different underlying integral types,
  90. //! the created range contains `integral_constant`s whose underlying
  91. //! type is their common type.
  92. //!
  93. //!
  94. //! Example
  95. //! -------
  96. //! @include example/range/make.cpp
  97. template <>
  98. constexpr auto make<range_tag> = [](auto const& from, auto const& to) {
  99. return range<implementation_defined>{implementation_defined};
  100. };
  101. #endif
  102. //! Alias to `make<range_tag>`; provided for convenience.
  103. //! @relates hana::range
  104. constexpr auto make_range = make<range_tag>;
  105. //! Shorthand to create a `hana::range` with the given bounds.
  106. //! @relates hana::range
  107. //!
  108. //! This shorthand is provided for convenience only and it is equivalent
  109. //! to `make_range`. Specifically, `range_c<T, from, to>` is such that
  110. //! @code
  111. //! range_c<T, from, to> == make_range(integral_c<T, from>, integral_c<T, to>)
  112. //! @endcode
  113. //!
  114. //!
  115. //! @tparam T
  116. //! The underlying integral type of the `integral_constant`s in the
  117. //! created range.
  118. //!
  119. //! @tparam from
  120. //! The inclusive lower bound of the created range.
  121. //!
  122. //! @tparam to
  123. //! The exclusive upper bound of the created range.
  124. //!
  125. //!
  126. //! Example
  127. //! -------
  128. //! @include example/range/range_c.cpp
  129. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  130. template <typename T, T from, T to>
  131. constexpr auto range_c = make_range(integral_c<T, from>, integral_c<T, to>);
  132. #else
  133. template <typename T, T from, T to>
  134. constexpr range<T, from, to> range_c{};
  135. #endif
  136. BOOST_HANA_NAMESPACE_END
  137. #endif // !BOOST_HANA_FWD_RANGE_HPP