I would like to serialize struct A where I could save the tag name of the enum expressions instead of its integer in a non intruisive way (without having to change struct A).
enum e_fruit {
apple,
banana,
coconut
};
struct A {
e_fruit fruit;
int num;
};
namespace boost { namespace serialization {
template<class Archive>
void serialize(Archive & ar, A &a, const unsigned int version)
{
ar & boost::serialization::make_nvp("FruitType", a.fruit); // this will store an integer
ar & boost::serialization::make_nvp("Number", a.num);
}
}}
I have tried introducing a lookup table locally to the serialize function :
template<class Archive>
void serialize(Archive & ar, A &a, const unsigned int version)
{
static const char* const labels[] = { "Apple", "Banana", "Coconut" };
ar & boost::serialization::make_nvp("FruitType", labels[a.fruit]); // this won't work
ar & boost::serialization::make_nvp("Number", a.num);
}
Unfortunately I get the error :
Error 78 error C2228: left of '.serialize' must have class/struct/union
since the prototype of make_nvp
is
nvp< T > make_nvp(const char * name, T & t){
return nvp< T >(name, t);
}
so T should be a deduced template argument. I then thought about making a structure which would contains these labels but then I would have to add this in struct A which is what I want to avoid...
So how can we achieve this in the most non-intruisive way ?
I think you need to separate loading from saving. This compiles, I wonder it it is worth the game...