]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
stdcountof-h: always return size_t
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 3 Jun 2025 01:58:24 +0000 (18:58 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 3 Jun 2025 01:58:24 +0000 (18:58 -0700)
* 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.

ChangeLog
lib/stdcountof.in.h

index fc7b56a69abcc2d2df1e5c11b548c8edf10c7218..799060377d053ce1b78255bb6c6e4a0d345b8045 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+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.
index af62b4d45bfefb8a2f55d23c61ee45cc64eb3e3d..6803b676bcf167e0cab7962b72ce40a127d424c5 100644 (file)
@@ -18,9 +18,9 @@
 #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.
@@ -33,7 +33,7 @@
      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
@@ -55,7 +55,7 @@ template <typename T>
 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>)
@@ -69,7 +69,7 @@ template <typename T>
 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).  */