c – 当遇到Nan和Inf时,提升序列化1.5.5崩溃

c – 当遇到Nan和Inf时,提升序列化1.5.5崩溃,第1张

概述似乎boost序列化无法从基于文本的归档中恢复值Nan和inf. 程序将终止,除非你在这种情况下处理archive_exception,任何解决方案? 图书馆 has this to say的作者: The simple truth is I never consider this. When it came up the last time I didn’t really think about 似乎boost序列化无法从基于文本的归档中恢复值Nan和inf.

程序将终止,除非你在这种情况下处理archive_exception,任何解决方案?

解决方法 图书馆 has this to say的作者:

The simple truth is I never consIDer this.

When it came up the last time I dIDn’t really think about it very much as I
was involved in other things and I hoped intereste[d] partIEs might come to a
consensus without my having to bend my over-@R_381_4041@ brain.

(goes on to discuss workarounds)

这似乎是正确的,在我的测试中只有二进制档案支持inf / nan.

除了nan / inf之外,Xml和文本存档都支持所有精度:

Live On Coliru

using BIA = boost::archive::binary_iarchive;using BOA = boost::archive::binary_oarchive;using TIA = boost::archive::text_iarchive;using TOA = boost::archive::text_oarchive;using XIA = boost::archive::xml_iarchive;using XOA = boost::archive::xml_oarchive;int main() {    // supported:    assert((perform_test<BIA,BOA,use_nan,use_inf,use_range>()));    assert((perform_test<XIA,XOA,no_nan,no_inf,use_range>()));    assert((perform_test<TIA,TOA,use_range>()));    // not supported:    assert(!(perform_test<XIA,use_inf>()));    assert(!(perform_test<TIA,use_inf>()));    assert(!(perform_test<XIA,no_inf>()));    assert(!(perform_test<TIA,no_inf>()));}

完整清单

后人:

#include <boost/archive/xml_oarchive.hpp>#include <boost/archive/xml_iarchive.hpp>#include <boost/archive/binary_oarchive.hpp>#include <boost/archive/binary_iarchive.hpp>#include <boost/archive/text_oarchive.hpp>#include <boost/archive/text_iarchive.hpp>#include <boost/serialization/vector.hpp>#include <sstream>using namespace boost::archive;static bool equal_or_nan(double a,double b) {    return (std::isnan(a) && std::isnan(b)) || a==b;}template <typename IA,typename OA,bool withNan   = true,bool withInf   = true,bool withRange = true>bool perform_test() {    std::vector<double> const v {        withRange? std::numeric_limits<double>::min()       : 0,withRange? std::numeric_limits<double>::max()       : 0,withRange? std::numeric_limits<double>::epsilon()   : 0,withNan?   std::numeric_limits<double>::quIEt_NaN() : 0,withInf?   std::numeric_limits<double>::infinity()  : 0,withInf? - std::numeric_limits<double>::infinity()  : 0,};    std::stringstream ss;    {        OA oa(ss);        oa << boost::serialization::make_nvp("element",v);    }    try    {        IA ia(ss);        std::vector<double> w;        ia >> boost::serialization::make_nvp("element",w);        return std::equal(v.begin(),v.end(),w.begin(),equal_or_nan);    } catch(...) {        return false;    }}static constexpr bool use_inf = true,use_nan = true,use_range = true;static constexpr bool no_inf  = false,no_nan = false,no_range = false;using BIA = boost::archive::binary_iarchive;using BOA = boost::archive::binary_oarchive;using TIA = boost::archive::text_iarchive;using TOA = boost::archive::text_oarchive;using XIA = boost::archive::xml_iarchive;using XOA = boost::archive::xml_oarchive;int main() {    // supported:    assert((perform_test<BIA,no_inf>()));}
总结

以上是内存溢出为你收集整理的c – 当遇到Nan和Inf时,提升序列化1.5.5崩溃全部内容,希望文章能够帮你解决c – 当遇到Nan和Inf时,提升序列化1.5.5崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1234326.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存