What I'm actually doing is trying to write a macro that will define a function like the following:
DEFINE_SERIALIZED_STRUCT(mystruct, (int, x)(int, y)(double, z)) | V struct mystruct { int x; int y; double z; }; void serialize(ostream & os, mystruct const & s) { os << "x: " << s.x << "\n"; os << "y: " << s.y << "\n"; os << "z: " << s.z << "\n"; }
(I didn't see a way to recover the field names from a Fusion-adapted struct -- from what I can tell, it more just seems to be a way to iterate over the fields.)
See this stackoverflow answer: http://stackoverflow.com/a/11744832/375343 It shows how to create reflectable fields in a class(or struct). It uses what is called "typed expressions" in the preprocessor(which is just an expression that puts the type in parenthesis). The macros shown for typed expressions(the `TYPEOF`, `STRIP`, and `PAIR` macros), however, won't work on msvc, but there are workarounds. Let me know if you are using msvc and I can send them to you. So in your example, you could define your struct like this: struct mystruct { REFLECTABLE ( (int) x, (int) y, (double) z ) }; Then write your serialization like this, which works for any struct that has reflectable fields: struct serialize_visitor { ostream & os; serialize_visitor(ostream & os) : os(os) {} template<class FieldData> void operator()(FieldData f) { os << f.name() << ": " << f.get() << std::endl; } }; template<class T> void serialize(ostream & os, T & x) { visit_each(x, serialize_visitor(os)); } Which I believe is much better than generating serialization code with the preprocessor, because it can be a pain trying to debug serialization code inside of a macro. Paul