<i id='YHuAq'><tr id='YHuAq'><dt id='YHuAq'><q id='YHuAq'><span id='YHuAq'><b id='YHuAq'><form id='YHuAq'><ins id='YHuAq'></ins><ul id='YHuAq'></ul><sub id='YHuAq'></sub></form><legend id='YHuAq'></legend><bdo id='YHuAq'><pre id='YHuAq'><center id='YHuAq'></center></pre></bdo></b><th id='YHuAq'></th></span></q></dt></tr></i><div id='YHuAq'><tfoot id='YHuAq'></tfoot><dl id='YHuAq'><fieldset id='YHuAq'></fieldset></dl></div>
  • <small id='YHuAq'></small><noframes id='YHuAq'>

    1. <legend id='YHuAq'><style id='YHuAq'><dir id='YHuAq'><q id='YHuAq'></q></dir></style></legend>
          <bdo id='YHuAq'></bdo><ul id='YHuAq'></ul>

      1. <tfoot id='YHuAq'></tfoot>
      2. 如何序列化 boost::dynamic_bitset?

        时间:2023-07-19
        <tfoot id='ASCf3'></tfoot>
      3. <legend id='ASCf3'><style id='ASCf3'><dir id='ASCf3'><q id='ASCf3'></q></dir></style></legend>
        • <bdo id='ASCf3'></bdo><ul id='ASCf3'></ul>

              <i id='ASCf3'><tr id='ASCf3'><dt id='ASCf3'><q id='ASCf3'><span id='ASCf3'><b id='ASCf3'><form id='ASCf3'><ins id='ASCf3'></ins><ul id='ASCf3'></ul><sub id='ASCf3'></sub></form><legend id='ASCf3'></legend><bdo id='ASCf3'><pre id='ASCf3'><center id='ASCf3'></center></pre></bdo></b><th id='ASCf3'></th></span></q></dt></tr></i><div id='ASCf3'><tfoot id='ASCf3'></tfoot><dl id='ASCf3'><fieldset id='ASCf3'></fieldset></dl></div>
                <tbody id='ASCf3'></tbody>

                  <small id='ASCf3'></small><noframes id='ASCf3'>

                  本文介绍了如何序列化 boost::dynamic_bitset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何使用 boost::dynamic_bitset 成员序列化类?

                  #include <boost/archive/text_oarchive.hpp>
                  #include <boost/archive/text_iarchive.hpp>
                  #include <boost/dynamic_bitset.hpp>
                  #include <boost/serialization/bitset.hpp>
                  #include <sstream>
                  
                  class A
                  {
                      friend class boost::serialization::access;
                      boost::dynamic_bitset<> x;
                      template<class Archive>
                      void serialize(Archive & ar, const unsigned int){
                          ar & x;
                      }
                  };
                  
                  int main()
                  {
                      A a;
                      std::stringstream ss;
                      boost::archive::text_oarchive oa(ss);
                      oa << a;
                      return 0;
                  }
                  

                  编译出错(boost 1.57)

                  In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37:0,
                                   from /usr/include/boost/archive/detail/oserializer.hpp:38,
                                   from /usr/include/boost/archive/detail/interface_oarchive.hpp:23,
                                   from /usr/include/boost/archive/detail/common_oarchive.hpp:22,
                                   from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
                                   from /usr/include/boost/archive/text_oarchive.hpp:31,
                                   from dynamic_bitset_setial.cpp:1:
                  /usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’:
                  /usr/include/boost/serialization/serialization.hpp:69:5:   instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
                  /usr/include/boost/serialization/serialization.hpp:128:9:   instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
                  /usr/include/boost/archive/detail/oserializer.hpp:148:5:   instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
                  dynamic_bitset_setial.cpp:25:1:   instantiated from here
                  /usr/include/boost/serialization/access.hpp:118:9: error: ‘class boost::dynamic_bitset<>’ has no member named ‘serialize’
                  

                  推荐答案

                  dynamic_bitset<> 不可序列化,正如您所发现的 (std::bitset 是不同的类型).

                  dynamic_bitset<> is not serializable, as you've found out (std::bitset<N> is different type).

                  不过不用担心,您可以毫不费力地添加它:

                  Not to worry, though, you can add it without too much effort:

                  namespace boost { namespace serialization {
                  
                      template <typename Ar, typename Block, typename Alloc>
                          void save(Ar& ar, dynamic_bitset<Block, Alloc> const& bs, unsigned) {
                              size_t num_bits = bs.size();
                              std::vector<Block> blocks(bs.num_blocks());
                              to_block_range(bs, blocks.begin());
                  
                              ar & num_bits & blocks;
                          }
                  
                      template <typename Ar, typename Block, typename Alloc>
                          void load(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned) {
                              size_t num_bits;
                              std::vector<Block> blocks;
                              ar & num_bits & blocks;
                  
                              bs.resize(num_bits);
                              from_block_range(blocks.begin(), blocks.end(), bs);
                              bs.resize(num_bits);
                          }
                  
                      template <typename Ar, typename Block, typename Alloc>
                          void serialize(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned version) {
                              split_free(ar, bs, version);
                          }
                  
                  } }
                  

                  这有效,例如生活在 Coliru

                  This works e.g. Live On Coliru

                  int main() {
                      A a;
                      for (int i=0; i<128; ++i)
                          a.x.resize(11*i, i%2);
                  
                      std::stringstream ss;
                      {
                          boost::archive::text_oarchive oa(ss);
                          oa << a;
                      }
                      std::cout << ss.str();
                      {
                          boost::archive::text_iarchive ia(ss);
                          A b;
                          ia >> b;
                  
                          assert(a.x == b.x);
                      }
                  }
                  

                  请注意,如果您负担不起复制块向量的费用,则直接在 m_bits 级别添加序列化同样容易,但这需要侵入性更改(至少需要好友访问).

                  Note that if you can't afford to copy the blocks vector, it's equally easy to add serialization directly on the m_bits level, but that requires intrusive changes (friend access required at a minimum).

                  这样的东西很容易被添加到拉取请求中.

                  Such a thing would easily be added to boost in a pull request.

                  这篇关于如何序列化 boost::dynamic_bitset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:理解 Boost.Spirit 中的列表运算符 (%) 下一篇:我可以使用 boost 库来执行跨平台应用程序吗?

                  相关文章

                1. <legend id='VuZeh'><style id='VuZeh'><dir id='VuZeh'><q id='VuZeh'></q></dir></style></legend>

                    <tfoot id='VuZeh'></tfoot>
                      <bdo id='VuZeh'></bdo><ul id='VuZeh'></ul>
                    1. <small id='VuZeh'></small><noframes id='VuZeh'>

                      <i id='VuZeh'><tr id='VuZeh'><dt id='VuZeh'><q id='VuZeh'><span id='VuZeh'><b id='VuZeh'><form id='VuZeh'><ins id='VuZeh'></ins><ul id='VuZeh'></ul><sub id='VuZeh'></sub></form><legend id='VuZeh'></legend><bdo id='VuZeh'><pre id='VuZeh'><center id='VuZeh'></center></pre></bdo></b><th id='VuZeh'></th></span></q></dt></tr></i><div id='VuZeh'><tfoot id='VuZeh'></tfoot><dl id='VuZeh'><fieldset id='VuZeh'></fieldset></dl></div>