提升序列化字符串 *
Boost serialize string *
我正在尝试在我的类中创建一个名为location的序列化函数,并在序列化后发送它。我不知道为什么我会收到错误,但也许是因为我正在尝试序列化字符串*。
下面是一个最小示例
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/serialization.hpp>
struct WithPointers {
WithPointers(const char* of = nullptr, const char* n = nullptr) {
if (n) name = new std::string(n);
}
WithPointers(WithPointers const&) = delete;
WithPointers& operator=(WithPointers const&) = delete;
~WithPointers() {
delete name;
}
std::string* name = nullptr;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & name;
}
};
struct WithoutPointers {
WithoutPointers(const char* of = nullptr, const char* n = nullptr) {
if (n) name = n;
}
std::string name;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & name;
}
};
#include <iostream>
#include <sstream>
int main()
{
std::string serialized;
typedef WithoutPointers Location; // DOESN'T COMPILE
//typedef WithPointers Location; // COMPILES
{
Location l1("da location", "da name");
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << l1;
serialized = oss.str();
}
std::cout << "serialized: '" << serialized << "'n";
}
如果我们使用WithoutPointers
它就会起作用
公平地说,我看不出这行不通的原因。在我看来,这是一种算术限制,甚至可能是一个错误。
相比之下,这里有一个简单地包装std::string
的版本:
struct XString {
std::string s;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & s;
}
};
有趣的是,使用它一切都按预期工作:
struct WithPointers {
WithPointers(const char* n = nullptr) {
if (n) name = new XString ({ n });
}
WithPointers(WithPointers const&) = delete;
WithPointers& operator=(WithPointers const&) = delete;
~WithPointers() {
delete name;
}
XString* name = nullptr;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & name;
}
};
在科里鲁现场观看
指纹:
serialized: '22 serialization::archive 11 0 0 1 1 0
0 7 da name'
Deserialized as 'da name'
总结
我仍然建议不要使用原始std::string*
.除此之外,您可能应该在Boost邮件列表中提及此问题
完整列表
供参考:
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/serialization.hpp>
struct XString {
std::string s;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & s;
}
};
struct WithPointers {
WithPointers(const char* n = nullptr) {
if (n) name = new XString ({ n });
}
WithPointers(WithPointers const&) = delete;
WithPointers& operator=(WithPointers const&) = delete;
~WithPointers() {
delete name;
}
XString* name = nullptr;
template <class Archive>
void serialize(Archive &ar, unsigned int) {
ar & name;
}
};
#include <iostream>
#include <sstream>
int main()
{
std::string serialized;
typedef WithPointers Location;
{
Location l1("da name");
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << l1;
serialized = oss.str();
}
std::cout << "serialized: '" << serialized << "'n";
{
Location l2;
std::istringstream iss(serialized);
boost::archive::text_iarchive ia(iss);
ia >> l2;
std::cout << "Deserialized as '" << l2.name->s << "'n";
}
}
相关文章:
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- POCO C++对象到 JSON 字符串序列化
- C++中的字符串序列化
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 在协议缓冲区 c++ 中反序列化字符串数组
- 如何将 boost::beast 中的序列化数据转换为字符串,以便我可以以 FIFO 方式处理它?
- c++ 序列化包含字符串和指向另一个对象的指针的对象
- 提升序列化提升进程间字符串
- 序列化的长字符串与快速JSON将被截断
- 使用 Cereal 反序列化 JSON 字符串
- 序列化结构以归档,并用字符串再次对其进行启用
- C++序列化只检索字符串的一半
- 对stl字符串和其他容器进行加密和序列化
- 如何在使用JSON boost序列化创建JSON字符串时获取名称-值对
- 如何在网络上序列化并发送JSOn字符串中的std::list
- 如何在不使用任何第三方库的情况下在 c++ 中反序列化 json 字符串
- 无法序列化和反序列化 Qt/C++ 中的字符串堆栈
- 提升序列化字符串 *
- xml序列化不同于以字符串形式发送的原因