boost.serialization并替换序列化std :: wstring的现有方法

时间:2010-09-02 16:55:45

标签: c++ serialization boost

我需要通过自己的方法序列化std :: wstring。 如何强制boost使用我的序列化方法而不是默认方法? 感谢。

1 个答案:

答案 0 :(得分:1)

未经测试,但您想为您的数据类型专门提供boost :: serialization :: archive:

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, std::wstring& s, const unsigned int version)
{
    for (std::wstring::iterator it = s.begin(); it != s.end(); ++it)
        ar >> *it
}

} // namespace serialization
} // namespace boost

此代码基本上应该按原样运行,您只想更改序列化函数的内容(但不是签名。)

为什么你想要以任何其他方式序列化wstring而不仅仅是打印它(即只使用普通的iostream),我不知道。