尝试序列化这个简单的类:
class Data
{
public:
Data();
Data(boost::uuids::uuid id);
Data(const Data&) = delete;
Data& operator=(const Data&) = delete;
inline boost::uuids::uuid getGuid() { return guid; }
template <class Archive>
void serialize(Archive & ar)
{
ar(guid);
}
private:
boost::uuids::uuid guid;
};
但我收到此错误消息
error C2338: Trying to serialize an unserializable type with an output archive.
指向 uuid。启用此功能的 boost 序列化方法是添加
#include <boost/uuid/uuid_serialize.hpp>
但这不适用于开箱即用的 Cereal 。 Cereal 文件说
所以我尝试在定义但没有运气的地方添加标题
#include <boost/uuid/uuid_io.hpp>
最佳答案
这适用于 Cereal JSON 文件。我还在评论中包含了一种针对二进制存档执行此操作的方法。
#ifndef CEREAL_TYPES_BOOST_UUID_
#define CEREAL_TYPES_BOOST_UUID_
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>
namespace cereal
{
template <class Archive> inline
void save(Archive& ar, boost::uuids::uuid const& uuid)
{
std::string val = boost::lexical_cast<std::string>(uuid);
ar(val);
// Other approach, probably better for binary
//ar(make_size_tag(static_cast<size_type>(uuid.size())));
//for (auto it = uuid.begin(), end = uuid.end(); it != end; ++it)
// ar(*it);
}
template <class Archive> inline
void load(Archive& ar, boost::uuids::uuid& uuid)
{
std::string val;
ar(val);
uuid = boost::lexical_cast<boost::uuids::uuid>(val);
// Other approach, probably better for binary
//size_type size;
//ar(make_size_tag(size));
//for (auto it = uuid.begin(), end = uuid.end(); it != end; ++it) {
// uint8_t val;
// ar(val);
// *it = val;
//}
}
} // namespace cereal
#endif // CEREAL_TYPES_BOOST_UUID_
关于c++ - 如何使用 Cereal 序列化 boost::uuid,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26989065/