0

I have an autogenerated structure type which is a container of various elements. Since the number of elements can vary (based on how the structure is generated), I would like to know (ideally in compilation time) the number of elements of currently generated structure.

Based on some input, the structure may look like this:

typedef struct _autoGenStruct {
    int a,
    int b,
    int c
} autogenStruct

or like this:

typedef struct _autoGenStruct {
    int a,
    int b,
    int c,
    int d
} autoGenStruct

In the first case, I expect to get 3 elements, in the second 4. The element type is always integer. Is there a way how to do this in C/C++? Thanks.

pisoir
  • 192
  • 3
  • 13
  • 1
    Considering that you really can't use the member count in any way, why do you need it? What problem is that counter supposed to solve? – Some programmer dude Jan 10 '20 at 08:42
  • 5
    As for a possible solution to the counter-problem, can't the program that does the auto-generation also include a macro or something that contains the count? – Some programmer dude Jan 10 '20 at 08:43
  • 1
    do you do the structure generation on your own or is it shipped to you? Since they are auto-generated, you can also auto-generate the control information (enums or constants containing the number of elements in each structure). – rhaport Jan 10 '20 at 08:45
  • [magic_get](https://github.com/apolukhin/magic_get) might interest you. – Jarod42 Jan 10 '20 at 08:46
  • Yes, that would be my idea as well, to include a counter to the generation of the structure. But before doing that, I was wondering if there is a direct way in C, without touching the generator. – pisoir Jan 10 '20 at 08:47
  • See https://stackoverflow.com/questions/35463646/arity-of-aggregate-in-logarithmic-time. Might be a good dupe. There's also https://github.com/felixguendling/cista/blob/master/include/cista/reflection/arity.h. – Max Langhof Jan 10 '20 at 09:11
  • Also, please clarify whether you need a solution that works in C++, or in C, or in both. Each of those cases would have wildly different answers. – Max Langhof Jan 10 '20 at 09:23
  • @MaxLanghof C++ is sufficient, but if there is a general solution which works in both cases, I will gladly see it. – pisoir Jan 10 '20 at 10:39

1 Answers1

3

If all elements are integers then you can probably assume there is no padding in your structures so the number of member variables should be simply:

auto elements = sizeof(_autoGenStruct) / sizeof(int);
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • it works only if all elements are of the same type. according to the topic owner the structure is container of various elements. They might be different – rhaport Jan 10 '20 at 08:46
  • 3
    @rhaport "The element type is always integer." – Jerome Reinländer Jan 10 '20 at 08:47
  • Nice. But how can I be sure that there is no padding in the structure. That depends on the compiler and the system used, or? But, yes, all elements are of the same int type. – pisoir Jan 10 '20 at 08:50
  • 1
    Even if there is no padding, there can be alignment requirements on the structure. Particularly if the elements are `short int` instead of `int` – Rishikesh Raje Jan 10 '20 at 08:51
  • 1
    Alignment is the main problem, e.g if sizeof(int) = 4 bytes and alignment = 8 bytes then this method will fail when the no of elements is odd. – Paul R Jan 10 '20 at 09:07
  • 1
    @pisoir It would be odd for a compiler to add padding between `int` subobjects because `int` is supposed to be the "natural" size for the architecture. But there's no formal guarantee in the standard regarding any sort of padding. – Max Langhof Jan 10 '20 at 09:09