I have the following template class:
#include
There's passthrough support for Boost Multiprecision serialization:
Classes
number,debug_adaptor,logged_adaptorandrational_adaptorhave "pass through" serialization support which requires the underlying backend to be serializable.Backends
cpp_int,cpp_bin_float,cpp_dec_floatandfloat128have full support for Boost.Serialization.
That is to say, it's supported iff the backend supports it: mailing list:
>Does/will multiprecesion have support for boost::serialization?Good question, and no not yet. I guess it should do though... it's hard though as some backends (for example GMP's mpf_t) don't round trip to and from string representations, and have an internal structure that probably shouldn't be relied upon :-
You can:
cpp_rational (as it is supported implicitly by the above doc excerpt)use raw mpz_* API to serialize, see e.g. How to serialize a GMP rational number?
you can explicitly add serialization support for the backend (note that using the gmp API's will be more efficient)
namespace boost { namespace serialization {
template
void save(Archive& ar, ::boost::multiprecision::backends::gmp_rational const& r, unsigned /*version*/)
{
std::string tmp = r.str(10000, std::ios::fixed);
ar & tmp;
}
template
void load(Archive& ar, ::boost::multiprecision::backends::gmp_rational& r, unsigned /*version*/)
{
std::string tmp;
ar & tmp;
r = tmp.c_str();
}
} }
BOOST_SERIALIZATION_SPLIT_FREE(::boost::multiprecision::backends::gmp_rational)
Here's a demo Live On Coliru with a simple roundtrip test.
#include
#include
#include
#include
#include
#include
namespace boost { namespace serialization {
template
void save(Archive& ar, ::boost::multiprecision::backends::gmp_rational const& r, unsigned /*version*/)
{
std::string tmp = r.str(10000, std::ios::fixed);
ar & tmp;
}
template
void load(Archive& ar, ::boost::multiprecision::backends::gmp_rational& r, unsigned /*version*/)
{
std::string tmp;
ar & tmp;
r = tmp.c_str();
}
} }
BOOST_SERIALIZATION_SPLIT_FREE(::boost::multiprecision::backends::gmp_rational)
#include
#include
#include
#include
#include
#include