From: Paul Eggert Date: Tue, 3 Jun 2025 01:58:24 +0000 (-0700) Subject: stdcountof-h: always return size_t X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a11064bb7c2e8a5d77cc4fdc9316ad2b2810e477;p=thirdparty%2Fgnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index fc7b56a69a..799060377d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2025-06-02 Paul Eggert + + 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 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 +# 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 template struct _gl_array_type_test { static const int is_array = 1; }; /* Bounded arrays. */ -template +template struct _gl_array_type_test { static const int is_array = 1; }; # define _gl_verify_is_array(a) \ sizeof (_gl_verify_type<_gl_array_type_test::is_array>) @@ -69,7 +69,7 @@ template template struct _gl_array_type_test { char small; }; /* Bounded arrays. */ -template +template struct _gl_array_type_test { char small; }; /* The T& parameter is essential here: it prevents decay (array-to-pointer conversion). */