C++ 使用std::array时出现输入流问题

C++ 使用std::array时出现输入流问题,c++,serialization,boost,boost-serialization,C++,Serialization,Boost,Boost Serialization,当struct A中没有bool b时,代码工作。当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
中没有
bool b
时,代码工作。当
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;
}
#包括
#包括
#包括
#包括
结构A
{
阵列天线;
布尔b;
模板无效序列化(存档&ar,常量未签名整数版本)
{
//ar&mat[0];
//ar&mat[1];
//ar&mat[2];
ar&mat;
ar&b;
}
};
int main()
{
{
std::string fname=“save.sr”;
std::ofs流(fname);
boost::archive::text\u oarchive oa(ofs);
A A;
oa>a;
}
返回0;
}

我看不出有什么问题。然后我查看了asan/ubsan,发现了域转换问题:

起初,我认为Boost[1]的特定版本中可能会有一个bug。然后我明白了

有一些良性警告:

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\u oarchive.hpp:65:50:运行时错误:加载值255,该值对于类型“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;
    }
};

[1]这是一场白费力气的追逐

  • 棒盒增压1.73
  • 棒盒增压1.72
  • 棒盒增压1.71
  • 棒盒增压1.70
  • 棒盒增压1.69
  • 棒盒增压1.68
  • 旺博克斯波索1.67
  • Wanbox Boost 1.66
  • 棒盒增压1.65.1
  • 棒盒增压1.65
  • Wandbox Boost 1.64.0
  • Wandbox Boost 1.63.0
  • 棒盒增压1.62
  • 棒盒增压1.61
  • 棒盒增压1.60

“ar&mat给出错误”请分享此错误。标题不允许我写“输入流错误”,让我写“输入流问题”。然后我忘了在内容中提到错误消息。我尝试初始化
std::array
,但从未想到bool可能是问题的原因。非常感谢。
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
"