Boost logo

Boost Users :

Subject: [Boost-users] {Serialization] Deserialization of const references
From: Mccall, Kurt E. (JSC-EG411) (kurt.e.mccall_at_[hidden])
Date: 2015-07-06 12:26:32


As near as I can tell from the documentation http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/serialization.html#references ,
the following is the way to handle const reference members, which are initialized in the constructor. Serialization seems to be working, from
an inspection of the resulting XML file, but after deserialization, the values in the object given by the const reference are wrong and are
all zero.

Here is my question: Is load_construct_data() below correct? It looks like it is initializing the constant reference from a temporary, but
I'm doing the same thing as the example in the documentation.

class MyClass1 {
    friend boost::serialization::access;
    …
};

class MyClass2 {
    friend boost::serialization::access;

    const MyClass1 & mc1_;

    MyClass2 ( const MyClass1 &mc1) : mc1_(mc1) { } ;
};

namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(
    Archive &ar, const MyClass2 *t, const unsigned int file_version)
{
        // mc1_ is a reference and requires special handling -- save it
        // as a pointer
        const MyClass1 *mc1 = &t->mc1_ ;

        ar << boost::serialization::make_nvp("mc1_", mc1);
}

template<class Archive>
inline void load_construct_data(
    Archive &ar, MyClass2* t, const unsigned int file_version)
{
    MyClass1 *mc1;
    ar >> boost::serialization::make_nvp("mc1_", mc1);

    // NOTE: IS THIS INITIALIZING THE REFERENCE FROM A TEMPORARY?
    ::new(t) MyClass2(*mc1);
}

Thanks,
Kurt



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