
Hi all, I am using the boost version 1.36.0 , linux platform. In my project i have to srialize a Union . i tried in the same way that classes and structures been serialized. But it gives a compilation error. How can i do that.. Please help me doing this. union myunion { uint32_t a; float t; }; i tried using like this. union myunion { uint32_t a; float t; template <class Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & a; ar & t; } i get an error when iwant to serialize the object of the union saying myunion is not a class or struct type. Pls help me to do this. thanks in Advance.

This was a surprise to me. Open up a Trak item and also mention which compiler you're using.
In the meantime you'll need to find a way to work around it. I know that serialization
works for boost:variant.
Robert Ramey
"niranjan bangera"

AMDG Robert Ramey wrote:
This was a surprise to me. Open up a Trak item and also mention which compiler you're using.
In the meantime you'll need to find a way to work around it. I know that serialization works for boost:variant.
Sure, but variant knows which type it contains. A union doesn't. In Christ, Steven Watanabe

Steven Watanabe wrote:
AMDG
niranjan bangera wrote:
union myunion {
uint32_t a; float t;
template <class Archive> void serialize(Archive & ar, const unsigned int file_version) {
ar & a; ar & t;
}
This is undefined behavior. a and t cannot both be valid at the same time.
LOL - makes me wonder if I'm even reading these emails. One could do try: struct myunion { union { uint32_t a; float t; } x; operator (uint32_t &)(){ // i forget the exact casting operator syntax return a; } operator(float &t)(){ return t; } template<class Archive> void serialize(Archive & ar, const unsigned int file_version){ ar & binary_object(sizeof(x), &x); } }; crude - but effective. Robert Ramey
In Christ, Steven Watanabe

Robert Ramey wrote:
Steven Watanabe wrote:
AMDG
niranjan bangera wrote:
union myunion {
uint32_t a; float t;
template <class Archive> void serialize(Archive & ar, const unsigned int file_version) {
ar & a; ar & t;
}
This is undefined behavior. a and t cannot both be valid at the same time.
LOL - makes me wonder if I'm even reading these emails.
One could do try:
struct myunion { union { uint32_t a; float t; } x; operator (uint32_t &)(){ // i forget the exact casting operator syntax return a; } operator(float &t)(){ return t; } template<class Archive> void serialize(Archive & ar, const unsigned int file_version){ ar & binary_object(sizeof(x), &x); } };
crude - but effective.
Robert Ramey
The best solutions would become more obvious if you know which type is valid at a higher level. For example, if you have a structure like this: struct UnionWrapper { myunion the_union; bool is_float; /* Set to true if and only if the float member is used */ template <class Archive> void serialize (Archive &ar, unsigned int const version) { ar & is_float; /*Do this first so deserialization knows which union member to get. */ if (is_float) { ar & the_union.t; } else { ar & the_union.a; } } };

This is what boost variant serialization does. Robert Ramey Andrew Holden wrote:
Robert Ramey wrote:
Steven Watanabe wrote:
AMDG
niranjan bangera wrote:
union myunion {
uint32_t a; float t;
template <class Archive> void serialize(Archive & ar, const unsigned int file_version) {
ar & a; ar & t;
}
This is undefined behavior. a and t cannot both be valid at the same time.
LOL - makes me wonder if I'm even reading these emails.
One could do try:
struct myunion { union { uint32_t a; float t; } x; operator (uint32_t &)(){ // i forget the exact casting operator syntax return a; } operator(float &t)(){ return t; } template<class Archive> void serialize(Archive & ar, const unsigned int file_version){ ar & binary_object(sizeof(x), &x); } };
crude - but effective.
Robert Ramey
The best solutions would become more obvious if you know which type is valid at a higher level. For example, if you have a structure like this:
struct UnionWrapper { myunion the_union; bool is_float; /* Set to true if and only if the float member is used */
template <class Archive> void serialize (Archive &ar, unsigned int const version) { ar & is_float; /*Do this first so deserialization knows which union member to get. */
if (is_float) { ar & the_union.t; } else { ar & the_union.a; } } };

Robert Ramey wrote:
This is what boost variant serialization does.
Robert Ramey
I suspected as much. Probably a bit more elegant than my brute force implementation though. I also suspect that OP already has something similar to that bool I added. If so, he doesn't need to add another bool to serialize the union; just make sure he serializes enough members to know which union member is valid before serializing that member. Either that or he uses the union to directly access the bits in the float. If that's the case, he can just serialize the float and be done with it. Andrew Holden wrote:
union myunion {
uint32_t a; float t; The best solutions would become more obvious if you know which type is valid at a higher level. For example, if you have a structure
niranjan bangera wrote: like this:
struct UnionWrapper { myunion the_union; bool is_float; /* Set to true if and only if the float member is used */
template <class Archive> void serialize (Archive &ar, unsigned int const version) { ar & is_float; /*Do this first so deserialization knows which union member to get. */
if (is_float) { ar & the_union.t; } else { ar & the_union.a; } } };
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Andrew Holden
-
niranjan bangera
-
Robert Ramey
-
Steven Watanabe