Does the boost serialization library support serialization of std::unique_ptr? I tried to compile the code below, but if I include the boost::archive::text_oarchive oa(ofs); oa << g; line,
the compiler (btw gcc4.7 with -std=c++11 flag) throws an error
/usr/include/boost/serialization/access.hpp:118:9: error: ‘class std::unique_ptr’ has no member named ‘serialize’
#include <iostream>
#include <memory>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
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;
{// compiler error, fine if commented out
boost::archive::text_oarchive oa(ofs); oa << g;
}
return 0;
}
See Question&Answers more detail:os