If your structs are POD then you might consider using std::tuple instead of structs. You could then use various template facilities to work through the members of the tuple.
Here is a simple example that prints the elements of a tuple - using boost::fusion::tuple instead of the std::tuple since it has many more tuple-manipulating facilities available:
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <iostream>
struct Printer {
template<typename T>
void operator()(const T &t) const {
std::cout << t << std::endl;
}
};
int main(int argc, const char * argv[])
{
boost::fusion::tuple<int, int, int, int, float> t =
boost::fusion::make_tuple(3, 5, 1, 9, 7.6f);
boost::fusion::for_each(t, Printer());
return 0;
}
You could include these in unions with the struct but you'd want to do some testing to ensure proper alignment agreement.
The upside is that these manipulations are very fast - most of the work is done at compile time. The down-side is that you can't use normal control structs like indexing with runtime indices - you'd have to build an abstraction layer around that as the normal get<i>(tuple) accessor requires that i be a compile time constant. Whether this is worth the complexity depends strongly on the application.
int Bar[2]- no.int Bar[3]- yes.vector<int>you will have to copy it into the vector for your "serialized" operations, and from the vector back into the structure for "normal" operations. You can cast the address of the structure intoint*, and then use that pointer in order to perform your "serialized" operations, which will take direct effect on the structure. BTW, what do you mean by "without the excessive use of the assignment operator"? You'll have to use the same amount of assignments regardless of how you represent this structure. Your purpose here is extremely vague, to be honest.std::tuple, which ties in with variadic templates for compile time dynamic manipulation. You may want to add a function to convert the relevant struct into astd::tuple.