如何能提高::系列化用的std ::从C ++ 11 shared_ptr的使用? [英] How can boost::serialization be used with std::shared_ptr from C++11?

查看:167
本文介绍了如何能提高::系列化用的std ::从C ++ 11 shared_ptr的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有升压模块为的系列化 HTTP ://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm相对=nofollow> 的boost :: shared_ptr的 ,但我找不到 的std :: shared_ptr的东西

I know that there is a Boost module for serialization of boost::shared_ptr, but I cannot find anything for std::shared_ptr.

另外,我不知道如何轻松实现它。我怕下面的code

Also, I don't know how to implement it easily. I'm afraid that the following code

namespace boost{namespace serialization{
template<class Archive, class T>
inline void serialize(Archive & ar, std::shared_ptr<T> &t, const unsigned int version)
{
  if(Archive::is_loading::value) {T*r;ar>>r;t=r;}
  else {ar<<t.get();}
}
}}//namespaces

不起作用。事实上,如果某个对象被提到多次,将它装入 AR&GT的第一次运行;&GT; R ,那只是一个指针后,将被复制。但是,我们将创造多个的shared_ptr 对象指向它,因此将它销毁一次以上。

doesn't work. Indeed, if some object was referred multiple times, it would be loaded with first run of ar>>r, and after that just a pointer will be copied. However we would create multiple shared_ptr objects pointing to it, and therefore would destruct it more than one time.

上任何想法?

约我使用该系统的一些技术细节:

Some technical details about the system I'm using:


  • 操作系统:Ubuntu的11.10(64)

  • 编译器:G ++(Ubuntu的/ Linaro的4.6.1-9ubuntu3)4.6.1

  • 增压版本:1.46.1(以安装命令和apt-get安装libboost-dev的

  • OS: Ubuntu 11.10 (x64)
  • Compiler: g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
  • boost version: 1.46.1 (installed with sudo apt-get install libboost-dev)

推荐答案

由于升压1.56,序列化库有<一个href=\"https://github.com/boostorg/serialization/blob/boost-1.56.0/include/boost/serialization/shared_ptr.hpp#L187\">built-in性病:: shared_ptr的支持。你并不需要实现自己的序列化的辅助功能,如果你可以使用库的更新版本。

As of Boost 1.56, the serialization library has built-in support for std::shared_ptr. You do not need to implement your own serialization helper functions if you can use a more recent version of the library.

这篇关于如何能提高::系列化用的std ::从C ++ 11 shared_ptr的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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