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

前端之家收集整理的这篇文章主要介绍了c – 当遇到Nan和Inf时,提升序列化1.5.5崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎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-stretched brain.

(goes on to discuss workarounds)

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

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

Live On Coliru

  1. using BIA = boost::archive::binary_iarchive;
  2. using BOA = boost::archive::binary_oarchive;
  3. using TIA = boost::archive::text_iarchive;
  4. using TOA = boost::archive::text_oarchive;
  5. using XIA = boost::archive::xml_iarchive;
  6. using XOA = boost::archive::xml_oarchive;
  7.  
  8. int main() {
  9.  
  10. // supported:
  11. assert((perform_test<BIA,BOA,use_nan,use_inf,use_range>()));
  12. assert((perform_test<XIA,XOA,no_nan,no_inf,use_range>()));
  13. assert((perform_test<TIA,TOA,use_range>()));
  14.  
  15. // not supported:
  16. assert(!(perform_test<XIA,use_inf>()));
  17. assert(!(perform_test<TIA,use_inf>()));
  18.  
  19. assert(!(perform_test<XIA,no_inf>()));
  20. assert(!(perform_test<TIA,no_inf>()));
  21.  
  22. }

完整清单

后人:

  1. #include <boost/archive/xml_oarchive.hpp>
  2. #include <boost/archive/xml_iarchive.hpp>
  3. #include <boost/archive/binary_oarchive.hpp>
  4. #include <boost/archive/binary_iarchive.hpp>
  5. #include <boost/archive/text_oarchive.hpp>
  6. #include <boost/archive/text_iarchive.hpp>
  7. #include <boost/serialization/vector.hpp>
  8. #include <sstream>
  9.  
  10. using namespace boost::archive;
  11.  
  12. static bool equal_or_nan(double a,double b) {
  13. return (std::isnan(a) && std::isnan(b)) || a==b;
  14. }
  15.  
  16. template <typename IA,typename OA,bool withNan = true,bool withInf = true,bool withRange = true>
  17. bool perform_test()
  18. {
  19. std::vector<double> const v {
  20. 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,};
  21.  
  22. std::stringstream ss;
  23. {
  24. OA oa(ss);
  25. oa << boost::serialization::make_nvp("element",v);
  26. }
  27.  
  28. try
  29. {
  30. IA ia(ss);
  31. std::vector<double> w;
  32. ia >> boost::serialization::make_nvp("element",w);
  33.  
  34. return std::equal(v.begin(),v.end(),w.begin(),equal_or_nan);
  35. } catch(...) {
  36. return false;
  37. }
  38. }
  39.  
  40. static constexpr bool use_inf = true,use_nan = true,use_range = true;
  41. static constexpr bool no_inf = false,no_nan = false,no_range = false;
  42.  
  43. using BIA = boost::archive::binary_iarchive;
  44. using BOA = boost::archive::binary_oarchive;
  45. using TIA = boost::archive::text_iarchive;
  46. using TOA = boost::archive::text_oarchive;
  47. using XIA = boost::archive::xml_iarchive;
  48. using XOA = boost::archive::xml_oarchive;
  49.  
  50. int main() {
  51.  
  52. // supported:
  53. assert((perform_test<BIA,no_inf>()));
  54.  
  55. }

猜你在找的C&C++相关文章