Is it possible to iterate over all elements in a struct or class?

c++, c++11

Solution

Nope, not with the language as it is.

You could do it by deriving your classes from a common base, and then implementing your own iterator to return pointers to each item as the iterator is traversed.

Alternatively put the items in a `std::vector` and use that to provide the iteration.

Problem

Is it possible to iterate over all elements in a struct or class? For example if I have a struct of three elements of different type: ``` struct A { classA a; classB b; classC c; }; ``` then I need some iterator such that a method next() would give me the value of the next element. The problem is that as you see, the values have different types.

Original source

Related problems