Hi Bruno! On Mon, Jun 02, 2025 at 01:28:31PM +0200, Bruno Haible wrote: > Hi Alejandro, > > > I've added the _Countof to ISO C2y and GCC 16. Clang 21 has also added > > it. There's a <stdcountof.h> compiler header with a countof macro too. > > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5> > > <https://thephd.dev/the-big-array-size-survey-for-c> > > <https://thephd.dev/the-big-array-size-survey-for-c-results> > > <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117025> > > <https://github.com/llvm/llvm-project/issues/102836> > > Thanks for working on this. It's a nice addition, to not have to write > #define SIZEOF(a) (sizeof(a)/sizeof(a[0])) > in many places.
Thank you! :-) > > > I see that gnulib uses the usual sizeof division often, so I was > > wondering if we should add the countof macro to gnulib with a fallback > > implementation when the system doesn't support it, and also use it > > within gnulib. > > I think we should do it, once the next version of ISO C is finalized. > > Why not already now? Because > > * The existing code > #define SIZEOF(a) (sizeof(a)/sizeof(a[0])) > will continue to work without problems. Thus, there is no hurry. Agree. > > * The standards authors may still change their minds. We've had a lot of discussion about this one. I doubt it will change again before release of C2y. > - We have seen ISO C changes even in the final stages of ISO C 23 > (remember the #b support?). > - The need for an extra header <stdcountof.h> is questionable, > because no existing code uses '_Countof' [1] The _Countof operator is available without any headers. The header is to provide the 'countof' non-uglified variant, which is in use in some projects. <https://codesearch.debian.net/search?q=%5Cbcountof%5Cb&literal=0> See the contents of the header file: alx@devuan:~/src/gnu/gcc/master$ tail gcc/ginclude/stdcountof.h <http://www.gnu.org/licenses/>. */ /* ISO C2Y: 7.21 Array count <stdcountof.h>. */ #ifndef _STDCOUNTOF_H #define _STDCOUNTOF_H #define countof _Countof #endif /* stdcountof.h */ > and we have seen > how <stdbool.h> was introduced and later made redundant. We have plans for making 'countof' available without the header in the future. However, we can't make it right now, because there are many projects using that identifier already. So, there'll be a transition period of one or two ISO C releases where <stdcountof.h> will be the way to get countof, just like <stdbool.h> was the way to use bool in C99 and C11. Then, maybe around C3y/C4x, we'll be able to take ownership of the name, and provide it without any headers, like C23 did with bool. Then, but not before, <stdcountof.h> will be redundant. I'm sure C2y will have <stdcountof.h>. > - How about the synchronization with ISO C++? I was disappointed to see > that code that uses 'unreachable' as defined by ISO C is not valid C++ > [2]. There are two reasons I didn't push too much for C++ to add this operator (I did suggest it, but soon stopped doing it): - I lost interest in C++ a long time ago. I don't want to have to program in C++. I don't like the language. - I have plans for improving _Countof as a GNU extension to work on array parameters to functions. I mean making this possible: wchar_t * wmemset(size_t n; wchar_t wcs[n], wchar_t wc, size_t n) { for (size_t i = 0; i < countof(wcs); i++) wcs[i] = wc; return wcs; } I would like to make that work as a GNU extension in GCC 17 or 18. I have also talked with Clang maintainers to agree on this feature. When this extension works in GCC and Clang, I'll propose it for standardization too, maybe in time for C2y, or maybe for C3x. What worries me of adding countof to C++ is that they may use it in some way that could preclude my planned extension. So I'll not remind C++ people about this until I made sure my extension is deployed in both GCC and Clang, and maybe in ISO C. Have a lovely day! Alex > > Bruno > > [1] https://codesearch.debian.net/search?q=%5Cb_Countof%5Cb&literal=0 > [2] https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00263.html > > > -- <https://www.alejandro-colomar.es/>
signature.asc
Description: PGP signature