Non-finite floating-point desalization fails even with matching faces

I need to use Boost.Serialization to serialize floating point numbers. Since NaN and infinity cannot be read from the input stream, I am trying to use faces in boost / math / special_functions. I tested them on my platform using code similar to the examples we can find here: http://www.boost.org/doc/libs/1_50_0/libs/math/doc/sf_and_dist/html/math_toolkit/utils/ fp_facets / intro.html However, the following code still cannot correctly uneserialize non-finite floating point values ​​(an exception is thrown with the description "input stream error").

#include <limits> #include <locale> #include <sstream> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/math/special_functions/nonfinite_num_facets.hpp> #include <boost/serialization/nvp.hpp> struct Data { float f; Data() : f(std::numeric_limits<float>::quiet_NaN()) {} template <class Archive> void serialize(Archive & ar, unsigned const) { ar & BOOST_SERIALIZATION_NVP(f); } }; void test() { using namespace boost::archive; Data d; std::ostringstream oss; xml_oarchive oar(oss); oar << BOOST_SERIALIZATION_NVP(d); //std::cout << oss.str() << std::endl; std::istringstream iss(oss.str()); std::locale const new_loc(iss.getloc(), new boost::math::nonfinite_num_get<char>); iss.imbue(new_loc); xml_iarchive iar(iss); iar >> BOOST_SERIALIZATION_NVP(d); std::cout << df << std::endl; } 

Am I doing something wrong? Is there a problem with my version of Boost or my platform? Is there a better solution? Any help would be greatly appreciated.

+2
source share
1 answer

I found a solution by reading the following implementation note: http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/implementation.html#charencoding

When creating an archive with the default flag, the stream locale changes to address encoding problems, but this mechanism can be disabled using the boost::archive::no_codecvt .

If I replace the string

 xml_iarchive iar(iss); 

with

 xml_iarchive iar(iss, no_codecvt); 

then it works.

+2
source

Source: https://habr.com/ru/post/982946/


All Articles