tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
size.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_ITERATOR_SIZE_HPP
5#define TETL_ITERATOR_SIZE_HPP
6
7#include <etl/_cstddef/ptrdiff_t.hpp>
8#include <etl/_cstddef/size_t.hpp>
9#include <etl/_type_traits/common_type.hpp>
10#include <etl/_type_traits/make_signed.hpp>
11#include <etl/_utility/ignore_unused.hpp>
12
13namespace etl {
14
15/// \brief Returns the size of the given container c or array array. Returns
16/// c.size(), converted to the return type if necessary.
17/// \ingroup iterator
18template <typename C>
19constexpr auto size(C const& c) noexcept(noexcept(c.size())) -> decltype(c.size())
20{
21 return c.size();
22}
23
24/// \ingroup iterator
25template <typename T, size_t N>
26constexpr auto size(T const (&array)[N]) noexcept -> size_t
27{
28 etl::ignore_unused(&array[0]);
29 return N;
30}
31
32/// \ingroup iterator
33template <typename C>
34constexpr auto ssize(C const& c) -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>
35{
36 using R = common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>;
37 return static_cast<R>(c.size());
38}
39
40/// \ingroup iterator
41template <typename T, ptrdiff_t N>
42constexpr auto ssize(T const (&array)[static_cast<size_t>(N)]) noexcept -> ptrdiff_t
43{
44 // The static_cast<size_t>(N) inside the array parameter is to keep gcc's
45 // sign-conversion warnings happy. Array sizes are of type size_t which
46 // triggers a signed to unsigned conversion in this case.
47 etl::ignore_unused(&array[0]);
48 return N;
49}
50
51} // namespace etl
52
53#endif // TETL_ITERATOR_SIZE_HPP
constexpr auto size(T const (&array)[N]) noexcept -> size_t
Definition size.hpp:26
constexpr auto ssize(T const (&array)[static_cast< size_t >(N)]) noexcept -> ptrdiff_t
Definition size.hpp:42
constexpr auto ssize(C const &c) -> common_type_t< ptrdiff_t, make_signed_t< decltype(c.size())> >
Definition size.hpp:34
constexpr auto size(C const &c) noexcept(noexcept(c.size())) -> decltype(c.size())
Returns the size of the given container c or array array. Returns c.size(), converted to the return t...
Definition size.hpp:19
Definition adjacent_find.hpp:9