-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_size.h
62 lines (51 loc) · 2.28 KB
/
array_size.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// (C) Copyright 2019 Xo Wang <[email protected]>
// SPDX-License-Identifier: Apache-2.0
// vim: et:sw=2:ts=2:tw=100
#ifndef MAYS_ARRAY_SIZE_H
#define MAYS_ARRAY_SIZE_H
#include <cstddef>
namespace mays {
namespace detail {
// Recursively reduces rank of the array type until either Rank = 0U, which returns the current
// dimension's bound, or the array type runs out of dimensions, which fails at the incomplete
// primary template.
template <typename T, unsigned Rank>
struct array_size; // base case (primary template) is undefined
template <typename T, size_t N>
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
struct array_size<T[N], 0U> {
static constexpr size_t value = N;
};
template <typename T, size_t N, unsigned Rank>
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
struct array_size<T[N], Rank> : array_size<T, Rank - 1> {};
template <typename T, unsigned Rank>
constexpr size_t array_size_v = array_size<T, Rank>::value;
} // namespace detail
// Gets the number of elements in any array that has a defined size. Unlike sizeof(array) /
// sizeof(array[0]), this will fail to compile if used with a pointer rather than an array. Unlike
// std::extent<decltype(array)>(), this will fail to compile rather than return 0 for arrays of
// unknown bound. For multi-dimensional arrays, this returns the bound of the (Rank - 1)th dimension
// (starting from the left).
//
// Examples:
// int a[2];
// constexpr size_t kArrayASize = mays::ArraySize(a); // kArrayASize = 2
// int b[3][2][1];
// constexpr size_t kArrayBSize = mays::ArraySize<2>(b); // kArrayBSize = 1
//
// Note that "array parameters" are simply annotated pointers and not truly array types:
// void Foo(int elements[3]) {
// // Does not compile because |elements| is (int*) not (int[3]).
// // |sizeof(elements) / sizeof(elements[0])| compiles but evaluates to probably 1 or 2
// // |std::extent_v<decltype(elements)>| compiles but evaluates to 0
// const size_t num_elements = mays::ArraySize(elements);
// }
template <size_t Rank = 0, typename T = void, size_t N = 0>
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
[[nodiscard]] constexpr size_t ArraySize(const T (&)[N]) {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
return ::mays::detail::array_size_v<T[N], Rank>;
}
} // namespace mays
#endif // MAYS_ARRAY_SIZE_H