c++ - 使用 std::array 时的输入流问题
问题描述
当没有bool b
instruct A
时,代码有效。什么时候bool b
出现,ar & mat
给出“输入流错误”,但一个一个地注册元素是std::array
有效的。这里有什么问题?
#include <fstream>
#include <boost/serialization/array.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
struct A
{
std::array<int, 3> mat;
bool b;
template<class Archive> void serialize(Archive& ar, const unsigned int version)
{
//ar & mat[0];
//ar & mat[1];
//ar & mat[2];
ar & mat;
ar & b;
}
};
int main()
{
{
std::string fname = "save.sr";
std::ofstream ofs(fname);
boost::archive::text_oarchive oa(ofs);
A a;
oa << a;
}
{
std::string fname = "save.sr";
std::ifstream ifs(fname);
boost::archive::text_iarchive ia(ifs);
A a;
ia >> a;
}
return 0;
}
解决方案
我没有看到有什么问题。然后我查看了 asan/ubsan,发现域转换问题:http ://coliru.stacked-crooked.com/a/edee75a22450d9ee
起初我以为在特定版本的 Boost[¹] 中可能会有一个错误。然后我恍然大悟。
有良性警告:
g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp -fsanitize=address,undefined -lboost_serialization && ./a.out
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct oserializer'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct iserializer'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct oserializer'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct iserializer'
/usr/local/include/boost/serialization/singleton.hpp:181:12: runtime error: reference binding to null pointer of type 'const struct extended_type_info_typeid'
然后我们得到了域错误:
/usr/local/include/boost/archive/text_oarchive.hpp:65:50: runtime error: load of value 255, which is not a valid value for type 'bool'
注意它是如何oarchive
进入它们的,并确认:
debug: "22 serialization::archive 17 0 0 0 0 3 -2049146224 32767 -2049145920 1
"
这些是……不确定的值。对于完整的积分域来说不是什么大问题,但对于 bool. 所以,修复你的初始化:
struct A {
std::array<int, 3> mat {};
bool b {};
template <class Ar> void serialize(Ar& ar, unsigned) {
ar & mat & b;
}
};
当然,你也可以使用其他样式:
std::array<int, 3> mat = {0,0,0};
bool b = false;
或者老式的构造函数:
std::array<int, 3> mat;
bool b;
A(std::array<int, 3> mat = {}, bool b = {}) : mat(mat), b(b) {}
等等,只要您确保初始化您的会员数据。
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/array.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <boost/version.hpp>
struct A {
std::array<int, 3> mat = {0,0,0};
bool b = false;
template <class Ar> void serialize(Ar& ar, unsigned) {
ar & mat & b;
}
};
int main() {
std::stringstream ss;
{
boost::archive::text_oarchive oa(ss);
A a;
oa << a;
}
std::cout << "BOOST_VERSION: " << BOOST_VERSION << "\n";
std::cout << "debug: " << std::quoted(ss.str()) << "\n";
{
boost::archive::text_iarchive ia(ss);
A a;
ia >> a;
}
}
印刷
BOOST_VERSION: 107500
debug: "22 serialization::archive 18 0 0 0 0 3 0 0 0 0
"
[¹] 这就是大雁追逐的样子
- Wandbox Boost 1.73没问题
- Wandbox Boost 1.72没问题
- Wandbox Boost 1.71仅警告
- Wandbox Boost 1.70相同的警告
- Wandbox Boost 1.69相同
- Wandbox Boost 1.68没问题
- Wandbox Bosot 1.67没问题
- Wanbox Boost 1.66没问题
- Wandbox Boost 1.65.1没问题
- Wandbox Boost 1.65没问题
- Wandbox Boost 1.64.0没问题
- Wandbox Boost 1.63.0没问题
- Wandbox Boost 1.62没问题
- Wandbox Boost 1.61没问题
- Wandbox Boost 1.60没问题
推荐阅读
- javascript - angular 6 如果 ID 匹配,则合并两个数组,并显示第一个数组中的所有数组
- java - 如何覆盖从父复杂类型继承的元素类型?
- regex - 没有特定单词的正则表达式匹配
- java - 绘制未按预期绘制矩形
- postgresql - 是否跨 CTE 优化了对 STABLE 函数的多次调用?
- wordpress - 如何返回新的 WordPress 5+ 库块的输出以自动将图像链接到媒体文件?
- ios - 我们可以让多个动态链接指向单个应用程序吗?
- c# - 如何在 Xamarin 应用程序的不同页面上将拆分编辑器字符串放入 ListView?
- java - 如何启用谷歌地方 API
- android - Android Azure Easy Tables 错误:不兼容的类型:MobileServiceTable
无法转换为列表