+2025-06-02 Paul Eggert <eggert@cs.ucla.edu>
+
+ stdcountof-h: always return size_t
+ * lib/stdcountof.in.h (__SIZE_TYPE__): Define to size_t,
+ including stddef.h to get it, if not already defined.
+ This avoids a bit of namespace pollution.
+ All uses of size_t changed to use __SIZE_TYPE__.
+ (countof): Return size_t, even if size_t is narrower than int (!).
+ While we’re at it, simplify ‘sizeof ((a)[0])’ to ‘sizeof *(a)’
+ as it’s simpler and later code uses the ‘*(a)’ notation already.
+
2025-06-02 Bruno Haible <bruno@clisp.org>
stdcountof-h: Tweaks.
#ifndef _GL_STDCOUNTOF_H
#define _GL_STDCOUNTOF_H
-#if defined __cplusplus
-/* Get size_t. */
+#ifndef __SIZE_TYPE__
# include <stddef.h>
+# define __SIZE_TYPE__ size_t
#endif
/* Returns the number of elements of the array A, as a value of type size_t.
void func (int a[10]) { ... }
*/
#define countof(a) \
- (sizeof (a) / sizeof ((a)[0]) + 0 * _gl_verify_is_array (a))
+ ((__SIZE_TYPE__) (sizeof (a) / sizeof *(a) + 0 * _gl_verify_is_array (a)))
/* Attempts to verify that A is an array. */
#if defined __cplusplus
template <typename T>
struct _gl_array_type_test<T[]> { static const int is_array = 1; };
/* Bounded arrays. */
-template <typename T, size_t N>
+template <typename T, __SIZE_TYPE__ N>
struct _gl_array_type_test<T[N]> { static const int is_array = 1; };
# define _gl_verify_is_array(a) \
sizeof (_gl_verify_type<_gl_array_type_test<decltype(a)>::is_array>)
template <typename T>
struct _gl_array_type_test<T[]> { char small; };
/* Bounded arrays. */
-template <typename T, size_t N>
+template <typename T, __SIZE_TYPE__ N>
struct _gl_array_type_test<T[N]> { char small; };
/* The T& parameter is essential here: it prevents decay (array-to-pointer
conversion). */