at.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. @file
  3. Forward declares `boost::hana::at` and `boost::hana::at_c`.
  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_AT_HPP
  9. #define BOOST_HANA_FWD_AT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <cstddef>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Returns the `n`th element of an iterable.
  15. //! @ingroup group-Iterable
  16. //!
  17. //! Given an `Iterable` and an `IntegralConstant` index, `at` returns the
  18. //! element located at the index in the linearization of the iterable.
  19. //! Specifically, given an iterable `xs` with a linearization of
  20. //! `[x1, ..., xN]`, `at(xs, k)` is equivalent to `xk`.
  21. //!
  22. //! If the `Iterable` actually stores the elements it contains, `at` is
  23. //! required to return a lvalue reference, a lvalue reference to const
  24. //! or a rvalue reference to the matching element, where the type of
  25. //! reference must match that of the iterable passed to `at`. If the
  26. //! `Iterable` does not store the elements it contains (i.e. it generates
  27. //! them on demand), this requirement is dropped.
  28. //!
  29. //!
  30. //! @param xs
  31. //! The iterable in which an element is retrieved. The iterable must
  32. //! contain at least `n + 1` elements.
  33. //!
  34. //! @param n
  35. //! A non-negative `IntegralConstant` representing the 0-based index of
  36. //! the element to return. It is an error to call `at` with an index that
  37. //! out of bounds of the iterable.
  38. //!
  39. //!
  40. //! Example
  41. //! -------
  42. //! @include example/at.cpp
  43. //!
  44. //!
  45. //! Benchmarks
  46. //! ----------
  47. //! <div class="benchmark-chart"
  48. //! style="min-width: 310px; height: 400px; margin: 0 auto"
  49. //! data-dataset="benchmark.at.compile.json">
  50. //! </div>
  51. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  52. constexpr auto at = [](auto&& xs, auto const& n) -> decltype(auto) {
  53. return tag-dispatched;
  54. };
  55. #else
  56. template <typename It, typename = void>
  57. struct at_impl : at_impl<It, when<true>> { };
  58. struct at_t {
  59. template <typename Xs, typename N>
  60. constexpr decltype(auto) operator()(Xs&& xs, N const& n) const;
  61. };
  62. constexpr at_t at{};
  63. #endif
  64. //! Equivalent to `at`; provided for convenience.
  65. //! @ingroup group-Iterable
  66. //!
  67. //!
  68. //! @note
  69. //! `hana::at_c<n>` is an overloaded function, not a function object.
  70. //! Hence, it can't be passed to higher-order algorithms. This is done
  71. //! for compile-time performance reasons.
  72. //!
  73. //!
  74. //! Example
  75. //! -------
  76. //! @include example/at_c.cpp
  77. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  78. template <std::size_t n>
  79. constexpr auto at_c = [](auto&& xs) {
  80. return hana::at(forwarded(xs), hana::size_c<n>);
  81. };
  82. #else
  83. template <std::size_t n, typename Xs>
  84. constexpr decltype(auto) at_c(Xs&& xs);
  85. #endif
  86. BOOST_HANA_NAMESPACE_END
  87. #endif // !BOOST_HANA_FWD_AT_HPP