Boost 序列化不适用于 shared_ptr<int>
Posted
技术标签:
【中文标题】Boost 序列化不适用于 shared_ptr<int>【英文标题】:Boost serialization doesn't work with shared_ptr<int> 【发布时间】:2015-08-17 19:21:02 【问题描述】:下面这段代码编译得很好:
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>
struct A
int i;
A(): i(0)
A(int i): i(i)
template <typename Archive>
void serialize(Archive& ar, const unsigned int)
ar & i;
;
int main()
auto a = std::make_shared<A>(465);
std::stringstream stream;
boost::archive::text_oarchive outstream;
out << a;
现在我希望如果我将 A
替换为 int
那么它也应该可以工作。
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>
int main()
auto a = std::make_shared<int>(465);
std::stringstream stream;
boost::archive::text_oarchive outstream;
out << a;
但是,此代码无法编译,但会导致断言失败:
In file included from main.cpp:1:
/usr/local/include/boost/serialization/shared_ptr.hpp:277:5: error: static_assert failed "boost::serialization::tracking_level< T >::value != boost::serialization::track_never"
BOOST_STATIC_ASSERT(
^
...
是我做错了什么还是 Boost 中的错误?
【问题讨论】:
@Yakk 如果您保存然后加载shared_ptr
,它们将指向不同的位置(显然因为您可能从不同的程序加载)。但是,如果您将shared_ptr
多次序列化为单个存档,然后加载它,则加载的指针将指向同一个位置。它也适用于裸指针和unique_ptr
s
【参考方案1】:
来自围绕该断言的 Boost 源代码:
// The most common cause of trapping here would be serializing
// something like shared_ptr<int>. This occurs because int
// is never tracked by default. Wrap int in a trackable type
BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
基本上,为了正确序列化shared_ptr
之类的东西,需要在序列化过程中集中跟踪指向的对象(以识别何时多个指针指向同一个对象,因此它们不会导致两个被序列化的对象的副本)。不过,跟踪一个对象比不跟踪它更昂贵,因此不会跟踪原始类型(假设会有很多 个对象)。从本质上讲,这使得在不使用 Boost 源的情况下序列化 shared_ptr<primitive_type>
是不可能的。正如评论所说,解决方案是序列化一些 UDT 包含 int。
【讨论】:
这方面的规范来源是doc/special.html#objecttracking(例如。“默认情况下,从不跟踪由实现级别类序列化特征指定的原始数据类型。如果希望跟踪共享通过指针的原始对象(例如用作引用计数的 long),它应该被包装在一个类/结构中,以便它是一个可识别的类型。改变 long 的实现级别的替代方法会影响序列化的所有 long整个程序 - 可能不是人们想要的。")以上是关于Boost 序列化不适用于 shared_ptr<int>的主要内容,如果未能解决你的问题,请参考以下文章
返回 boost::shared_ptr 和从返回的原始指针构造 boost::shared_ptr 有啥区别?
C++ ReadConsoleInput 不适用于 boost::thread