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