提高mpfr_float系列化 [英] boost serialization of mpfr_float

查看:161
本文介绍了提高mpfr_float系列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想序列包含的boost ::多precision :: mpfr_float作为成员的自定义类。在这里说的Boost.Serialization文档中一个类型 T 是序列化的当且仅当5个属性中的至少一个是真的,<一个href=\"http://www.boost.org/doc/libs/1_57_0/libs/multi$p$pcision/doc/html/boost_multi$p$pcision/tut/serial.html\"相对=nofollow>这里在该数量类有需要的底层后端可序列化的直通支持多precision文档。

I would like to serialize a custom class containing an boost::multiprecision::mpfr_float as a member. It says here in the Boost.Serialization documentation that a type T is serializable iff at least one of 5 properties is true, and here at the Multiprecision documentation that the number class has pass-through support which requires the underlying backend to be serializable.

有关Boost.Multi precision的 mpfr_float 类,我知道:

For Boost.Multiprecision's mpfr_float type, I know:


  1. 这不是一个原始类型。

  2. 这是一个类类型,但它并没有定义的连载功能(S)。

  3. 这不是一个指针序列化的类型。

  4. 这不是一个序列化类型的引用。

  5. 这不是一个Serializable类型的本地C ++数组。

  1. It is not a primitive type.
  2. It is a class type, but it doesn't have the serialize function(s) defined.
  3. It is not a pointer to a Serializable type.
  4. It is not a reference to a Serializable type.
  5. It is not a native C++ array of Serializable type.

所以,它看起来像如果我想序列化mpfr_float类型,我必须为这种类型的连载功能。

So, it looks like if I want to serialize the mpfr_float type, I must provide the serialize function for that type.

我的问题是:我如何延长 mpfr_float 键入要写入序列化的连载函数自己?我想我需要访问后端MPFR,并发挥与底层数据,我不能确定如何进行。从有经验的人升压序列化previously,序列化的类提示将不胜AP preciated。

My question is this: How can I extend the mpfr_float type to be serializable by writing the serialize function myself? I think I need to access the mpfr backend, and play with the underlying data, and I am unsure how to proceed. Tips from someone with experience Boost serializing previously-unserialized classes would be greatly appreciated.

结论的解决方案

,我来到了一个解决方案,往返只需用precisions罚款100和1000:

Based on the reply from sehe, I arrived at a solution which round-trips just fine with precisions 100 and 1000:

namespace boost { namespace serialization { // insert this code to the appropriate namespaces


/**
 Save a mpfr_float type to a boost archive.
 */
template <typename Archive>
void save(Archive& ar, ::boost::multiprecision::backends::mpfr_float_backend<0> const& r, unsigned /*version*/)
{
    std::string tmp = r.str(0, std::ios::fixed);// 0 indicates use full precision
    ar & tmp;
}

/**
 Load a mpfr_float type from a boost archive.
 */
template <typename Archive>
void load(Archive& ar, ::boost::multiprecision::backends::mpfr_float_backend<0>& r, unsigned /*version*/)
{
    std::string tmp;
    ar & tmp;
    r = tmp.c_str();
}

} } // re: namespaces

该解决方案解决了从项目需要上述(2),这表明有必要添加连载功能。感谢您的帮助。

This solution addresses the need from item (2) above, which indicated the need to add the serialize functions. Thanks for the help.

推荐答案

的直通支持,意味着你必须添加序列化了的后台的类型,确实如此。

The passthrough support implies that you have to add the serialization for the backend type, indeed.

您可以使用同样的方法,因为我在这个答案显示:

You can use the same approach as I showed in this answer:

  • How to de/serialize a map with template class using boost::multiprecision::mpq_rational

在这里我展示如何(德)序列化 mpq_rational

where I show how to (de)serialize mpq_rational

这篇关于提高mpfr_float系列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆