gpt4 book ai didi

c++ - 使用 boost serialize 的 Microsoft GUID 序列化?

转载 作者:行者123 更新时间:2023-11-27 23:51:24 31 4
gpt4 key购买 nike

我有以下内容:

struct Member
{
GUID id;
int extra;

template<class Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & id;
ar & extra;
}

}

当我编译代码时出现以下编译错误:

错误 25 error C2039: 'serialize' : is not a member of '_GUID'

我如何为 Microsoft GUID 专门化 boost 序列化?

最佳答案

提示:假设UUID类型是POD,可以使用make_binary_object

这是 UUID 作为 POD 的模型类型:

#include <array>

using UUID = std::array<uint8_t, 16>; // mock up
static_assert(std::is_pod<UUID>(), "assumes UUID is POD");

任何 POD 都可以。例如。 boost::uuids::uuid¹ 也是 POD。与 struct UUID { char data[16]; 相同

非侵入式序列化:

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/binary_object.hpp>

namespace boost { namespace serialization {
template <typename Ar>
void serialize(Ar& ar, UUID& u, unsigned /*version*/) {
ar & make_binary_object(&u, sizeof(u));
}
} }

演示

Live On Coliru

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

int main() {
std::stringstream ss;
{
boost::archive::text_oarchive oa(ss);
UUID u {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}};
oa << u;
}

std::cout << ss.str() << "\n";

{
boost::archive::text_iarchive ia(ss);
UUID u {};
ia >> u;

std::copy(begin(u), end(u), std::ostream_iterator<int>(std::cout << "roundtripped: ", " "));
}
}

¹ 虽然该库已经在 boost/uuid/uuid_serialize.hpp header 中定义了序列化

关于c++ - 使用 boost serialize 的 Microsoft GUID 序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139744/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com