1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: Gets the number of elements in the array
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_UTILITY_ELEMENT_NUM_H
13 #define EXTL_UTILITY_ELEMENT_NUM_H
15 /*!\file element_num.h
16 * \brief Gets the number of elements in the array
19 /* ///////////////////////////////////////////////////////////////////////
22 #include "static_assert.h"
23 #include "../type/typedef.h"
25 /* ///////////////////////////////////////////////////////////////////////
30 /* //////////////////////////////////////////////////////////////////// */
31 #if defined(__cplusplus) && \
32 defined(EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT)
34 # if defined(EXTL_COMPILER_IS_GCC)
36 # pragma pack(push, 1)
37 template < e_uint_t N
>
38 struct extl_array_size
44 template < typename_param_k T
, e_uint_t N
>
45 extl_array_size
<N
> const& extl_static_array_size(T (&)[N
]);
48 # define EXTL_ELEMENT_NUM(ar) sizeof(EXTL_NS(extl_static_array_size)(ar))
51 template < e_uint_t N
>
52 struct extl_array_size
57 /* Passing array by reference will not cause pointer decay */
58 template < typename_param_k T
, e_uint_t N
>
59 extl_array_size
<N
> const& extl_static_array_size(T (&)[N
]);
62 # define EXTL_ELEMENT_NUM(ar) sizeof(EXTL_NS(extl_static_array_size)(ar).c)
63 # endif /* EXTL_COMPILER_IS_GCC */
65 /*!\brief EXTL_ELEMENT_NUM does'nt support array-to-pointer decay
68 * int *p=ar; //or int *p=&ar[0];
69 * EXTL_ELEMENT_NUM(ar) == 10
70 * EXTL_ELEMENT_NUM(p) ?= 1
72 # define EXTL_ELEMENT_NUM(ar) (sizeof(ar) / sizeof(0[(ar)]))
73 #endif /* defined(__cplusplus) && \
74 defined(EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT) */
76 /* ///////////////////////////////////////////////////////////////////////
79 #ifdef EXTL_UTILITY_ELEMENT_NUM_TEST_ENABLE
80 # include "unit_test/element_num_test.h"
82 /* ///////////////////////////////////////////////////////////////////////
87 /* //////////////////////////////////////////////////////////////////// */
88 #endif /* EXTL_UTILITY_ELEMENT_NUM_H */
89 /* //////////////////////////////////////////////////////////////////// */