On 6/2/25 15:50, Bruno Haible via Gnulib discussion list wrote:
+  (sizeof (a) / sizeof ((a)[0]) + 0 * _gl_verify_is_array (a))

As a minor point, that expression has the wrong type on theoretical (but standard-conforming) platforms where size_t is narrower than int. I installed the attached to address this, plus avoid a bit of namespace pollution.
From a11064bb7c2e8a5d77cc4fdc9316ad2b2810e477 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 2 Jun 2025 18:58:24 -0700
Subject: [PATCH] stdcountof-h: always return size_t
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 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           | 11 +++++++++++
 lib/stdcountof.in.h | 10 +++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fc7b56a69a..799060377d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2025-06-02  Paul Eggert  <egg...@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  <br...@clisp.org>
 
 	stdcountof-h: Tweaks.
diff --git a/lib/stdcountof.in.h b/lib/stdcountof.in.h
index af62b4d45b..6803b676bc 100644
--- a/lib/stdcountof.in.h
+++ b/lib/stdcountof.in.h
@@ -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).  */
-- 
2.49.0

Reply via email to