at.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. @file
  3. Forward declares `boost::hana::at` and `boost::hana::at_c`.
  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_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. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  44. constexpr auto at = [](auto&& xs, auto const& n) -> decltype(auto) {
  45. return tag-dispatched;
  46. };
  47. #else
  48. template <typename It, typename = void>
  49. struct at_impl : at_impl<It, when<true>> { };
  50. struct at_t {
  51. template <typename Xs, typename N>
  52. constexpr decltype(auto) operator()(Xs&& xs, N const& n) const;
  53. };
  54. constexpr at_t at{};
  55. #endif
  56. //! Equivalent to `at`; provided for convenience.
  57. //! @ingroup group-Iterable
  58. //!
  59. //!
  60. //! @note
  61. //! `hana::at_c<n>` is an overloaded function, not a function object.
  62. //! Hence, it can't be passed to higher-order algorithms. This is done
  63. //! for compile-time performance reasons.
  64. //!
  65. //!
  66. //! Example
  67. //! -------
  68. //! @include example/at_c.cpp
  69. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  70. template <std::size_t n>
  71. constexpr auto at_c = [](auto&& xs) {
  72. return hana::at(forwarded(xs), hana::size_c<n>);
  73. };
  74. #else
  75. template <std::size_t n, typename Xs>
  76. constexpr decltype(auto) at_c(Xs&& xs);
  77. #endif
  78. BOOST_HANA_NAMESPACE_END
  79. #endif // !BOOST_HANA_FWD_AT_HPP