I have a structure
typedef struct A
{
int a;
int b;
char * c;
}aA;
I want to iterate over each an every member of the structure
C++ does not support reflection out of the box, so what you are asking for is impossible within the core language.
Various libraries try to provide such functionality, typically by making you register your fields through some methods or macros, which will behind the scene save them into a collection of some sort, on which you can iterate.
can I iterate over the members of a struct in standard c++?
No, standard c++ doesn't provide a method for accomplishing what you are asking for, you "iterate" elements of a container - you cannot iterate over members of a certain type.
"reflection" (as this type of feature is most often referred to isn't part of C++).
in c++11 you could use a std::tuple<int,int,char*>
instead of your struct A
, it will store the same kind of elements and is far easier to iterate over (using some template magic).
the elements won't have names ranging from 'a'
to 'c'
but if you'd like to print it that way this of course can be accomplished by some extra lines of code.
To access a specific element you'll use std::get<N> (your_tuple)
where N
is a integral ranging from 0 to std::tuple_size<std::tuple<int,int,char*>>::value - 1
(ie. 2).