as mentioned by pmr , I managed to find the following solution, and at first glance everything works. In the hope that someone will find this useful:
#include <iostream> #include <memory> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> namespace boost { namespace serialization { template<class Archive, class T> inline void save( Archive & ar, const std::unique_ptr< T > &t, const unsigned int /*file_version*/ ){ // only the raw pointer has to be saved const T * const base_pointer = t.get(); ar & BOOST_SERIALIZATION_NVP(base_pointer); } template<class Archive, class T> inline void load( Archive & ar, std::unique_ptr< T > &t, const unsigned int /*file_version*/ ){ T *base_pointer; ar & BOOST_SERIALIZATION_NVP(base_pointer); t.reset(base_pointer); } template<class Archive, class T> inline void serialize( Archive & ar, std::unique_ptr< T > &t, const unsigned int file_version ){ boost::serialization::split_free(ar, t, file_version); } } // namespace serialization } // namespace boost class MyDegrees { public: void setDeg(int d){deg = d;} int getDeg()const {return deg;} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & deg; } int deg; }; class gps_position { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & degrees; } std::unique_ptr<MyDegrees> degrees; public: gps_position(): degrees(std::unique_ptr<MyDegrees>(new MyDegrees)){}; void setDeg(int d){degrees->setDeg(d);} int getDeg() const {return degrees->getDeg();} }; int main() { std::ofstream ofs("filename"); gps_position g; g.setDeg(45); std::cout<<g.getDeg()<<std::endl; { boost::archive::text_oarchive oa(ofs); oa << g; } gps_position newg; { std::ifstream ifs("filename"); boost::archive::text_iarchive ia(ifs); ia >> newg; std::cout<<newg.getDeg()<<std::endl; } return 0; }
dodol source share