Boost logo

Boost Users :

Subject: Re: [Boost-users] serilalization of union
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-01-07 13:09:53


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net