c++boost-serializationboost-optional

boost::serialization of boost::optional of type with private default constructor


I'm upgrading from boost 1.54 to the latest 1.80 and have a compilation problem with boost serialization.

I have a class A with private default constructor. Another class B has a boost::optional<A> field and also is boost::serializable. To allow boost::serialization to create an empty instance of A during boost::serialization, I had friend class boost::serialization::access within A. It worked with boost 1.54, because that version of boost used access::construct<T>() to create an instance and so it respected my friendship declaration. In 1.80 in contrast the instance of optional<T> is initialized simply as t = T(), which obviously does not work if T has private default constructor.

Is it simply a regression by oversight, or is there some deep thought behind the breaking change? And more importantly what is the recommended way of serializing boost::optional<T>, where T has a private default constructor?


Solution

  • I have not found any better solution than to add the following friendship declaration to my class A, in addition to the existing friend class boost::serialization::access declaration:

    template<class Archive, class T>
    friend void boost::serialization::load(
       Archive & ar, boost::optional<T> & t, const unsigned int version);