Hi Bruno,

On Tue, Jun 03, 2025 at 12:00:22AM +0200, Bruno Haible wrote:
> Alejandro Colomar wrote:
> >     # define countof(a)  (sizeof(a) / sizeof((a)[0]) + must_be(is_array(a)))
> >     #endif
> > 
> > must_be(is_array(a)) is a trick to assert that the argument is an array.
> > <https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c/57537491#57537491>
> 
> Now that is very nice! The ability to detect wrong use of function parameters
> is valuable already now. You referenced <https://lkml.org/lkml/2015/9/3/428>.
> 
> I'm therefore adding a module 'stdcountof-h' already now; see below.

Nice!  Thanks!  :-)

(A mention in the commit message --maybe a Suggested-by tag-- would have
 been nice too.)  :)

> 
> > > While I understand this, it still means that gnulib — which strives to 
> > > make
> > > the same code usable in C++ like in C — will probably have to override
> > > <stdcountof.h> roughly like this:
> > > 
> > >   #ifdef __cplusplus
> > >   template <typename T, size_t N>
> > >   constexpr size_t countof(T const (&)[N]) {
> > >     return N;
> > >   }
> > >   #else
> > >   # define countof _Countof
> > >   #endif
> > 
> > You'll need to take into account the case where _Countof is not
> > available, and fall back to sizeof division
> 
> Yeah. In the end I ended up not using _Countof at all, because when
> compilers will have _Countof they will most likely also have <stdcountof.h>.

Yup.

> 
> >     #ifdef __cplusplus
> >     template <typename T, size_t N>
> >     constexpr size_t countof(T const (&)[N]) {
> >       return N;
> >     }
> 
> It turns out it's not that easy: Older C++ compilers reject function
> parameters of type   T (&) [N]. So, I've used the same approach for
> C++ as for C. [As usual, C++ is a big time sink.]

Hmmm.

> 
> > testsuite that I added in GCC for the operator.
> > 
> >     $ find gcc/testsuite/ | grep countof
> >     gcc/testsuite/gcc.dg/countof-pedantic.c
> >     gcc/testsuite/gcc.dg/countof-zero.c
> >     gcc/testsuite/gcc.dg/countof-no-compat.c
> >     gcc/testsuite/gcc.dg/countof-compat.c
> >     gcc/testsuite/gcc.dg/countof-vla.c
> >     gcc/testsuite/gcc.dg/countof-vmt.c
> >     gcc/testsuite/gcc.dg/countof.c
> >     gcc/testsuite/gcc.dg/countof-compile.c
> >     gcc/testsuite/gcc.dg/countof-pedantic-errors.c
> >     gcc/testsuite/gcc.dg/countof-stdcountof.c
> >     gcc/testsuite/gcc.dg/countof-zero-compile.c
> 
> There's no test case for countof(nullptr) in there. That's not a bug,
> just not 100% test coverage.

Hmmm, makes sense.  On the other hand, I think I'll continue behaving as
if nullptr had never existed.  :p

> 
> 
> 2025-06-02  Bruno Haible  <[email protected]>
> 
>       stdcountof-h: Add tests.
>       * tests/test-stdcountof-h.c: New file.
>       * tests/test-stdcountof-h-c++.cc: New file.
>       * modules/stdcountof-h-tests: New file.
>       * modules/stdcountof-h-c++-tests: New file.
> 
>       stdcountof-h: New module.
>       * lib/stdcountof.in.h: New file.
>       * m4/stdcountof_h.m4: New file.
>       * modules/stdcountof-h: New file.

Minor issue:

        An <stdcountof.h> that is like C23.

That should say C2y; C23 didn't have <stdcountof.h>.

BTW, do you have in plans for using this module within gnulib itself?


Have a lovely night!
Alex

-- 
<https://www.alejandro-colomar.es/>

Attachment: signature.asc
Description: PGP signature

Reply via email to