增强序列化std :: unique_ptr支持 [英] boost serialization std::unique_ptr support

查看:79
本文介绍了增强序列化std :: unique_ptr支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost序列化库是否支持std :: unique_ptr的序列化? 我尝试编译下面的代码,但是如果包含 boost :: archive :: text_oarchive oa(ofs); oa<< g; 行,

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,

编译器(带有-std = c ++ 11标志的btw gcc4.7)抛出错误

the compiler (btw gcc4.7 with -std=c++11 flag) throws an error

/usr/include/boost/serialization/access.hpp:118:9:错误:类std :: unique_ptr"没有名为序列化"的成员

#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;
}

推荐答案

否,没有开箱即用的适应方法.您将需要自己提供一个非侵入式适配器.请参阅此处的教程,以了解如何执行此操作.

No, there is no out of the box adaption. You will need to provide a non-intrusive adapter yourself. See the tutorial here to get an idea how to do that.

这篇关于增强序列化std :: unique_ptr支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆