tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
index.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CONTAINER_INDEX_HPP
5#define TETL_CONTAINER_INDEX_HPP
6
7#include <etl/_concepts/emulation.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_cstddef/ptrdiff_t.hpp>
10#include <etl/_iterator/begin.hpp>
11#include <etl/_iterator/end.hpp>
12#include <etl/_utility/forward.hpp>
13
14namespace etl::detail {
15
16/// \brief Index a range doing bound checks in debug builds
17/// Copied from https://github.com/gnzlbg/static_vector
18template <typename Range, typename Index>
19 requires(RandomAccessRange<Range>)
20constexpr auto index(Range&& rng, Index&& i) noexcept -> decltype(auto)
21{
22 using etl::begin;
23 using etl::end;
24
25 TETL_PRECONDITION(static_cast<etl::ptrdiff_t>(i) < (end(rng) - begin(rng)));
26 return begin(etl::forward<Range>(rng))[etl::forward<Index>(i)];
27}
28} // namespace etl::detail
29
30#endif // TETL_CONTAINER_INDEX_HPP
Definition adjacent_find.hpp:9