On Wed, 14 Aug 2024 23:10:17 +0200
Alejandro Colomar via Gcc <gcc@gcc.gnu.org> wrote:

> -  Rename lengthof => elementsof.  Aaron found incompatible existing
>    functions called lengthof() in the wild.

Hi Alejandro, 

I have a suggestion and an open-ended question.  

The suggestion: Instead of elementsof, name it "countof".  

I use in my code

        #define COUNT_OF(A) sizeof(A) / sizeof(*(A))

i don't know why anyone does anything else.  It sure would be nice to
have an integrated solution, though, so A could be either an array or a
pointer.  That's been a bugaboo of mine for years.  

I don't think "elementsof" is a good name because it doesn't return
elements.  sizeof returns a size; countof returns a count.
"elementsof" is plural, so should return elements, plural, not a
number.  

If "count" is not acceptable for some reason, then "nelemof" would at
least say what it means, albeit awkwardly.

My question regards metadata more generally.  I would sincerely like to
have a way to iterate over structure members.  That is, given 

        struct T_t { int i; char *s; };

I would like to be able to do something like 

        forall member in T_t:
                print offset, size, type, name // of T_t
        }

producing

        0, 4, int, i
        4, 8, char*, s

The user would use that information for I/O as kind of poor man's
iostream.  Put the loop in macro, and you have an instant, consistent
way to serialize and deserialize C structs.  

Has anything like that been discussed for C at the standards level, as
far as you know?  

Kind regards, 

--jkl

Reply via email to