Boost logo

Boost Users :

Subject: [Boost-users] Customizing locale of stream for Boost.Serialization seems to fail
From: Christophe Fuzier (fuzierc_at_[hidden])
Date: 2014-03-10 06:06:24


I have a problem with Boost.Serialization. I have posted a question on a
forum but have not got any answer until now. Here is the reproduction of my
post (note: I'm using Boost 1.48, patch level 0).

I need to use Boost.Serialization to serialize floating-point numbers.
Since NaN and infinites cannot natively be read from an input stream, I am
trying to use the facets in boost/math/special_functions. I have tested
them on my platform using code similar to the examples we can find here:

However, the following code still fails to properly unserialize non-finite
floating point values (an exception is thrown with 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
        xml_iarchive iar(iss);
        iar >> BOOST_SERIALIZATION_NVP(d);
        std::cout << d.f << std::endl;

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

Thank you in advance for any suggestion.

Christophe Fuzier

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at