c++ - 从模板化类版本控制派生类的序列化

标签 c++ serialization boost versioning

我正在尝试序列化从模板类派生的类,我在其中修复了模板参数。我想独立于派生类序列化基类,以防我将模板固定为其他类型,并且我希望派生类序列化为基类和其他一些数据。当我在我的最小示例中执行此操作时:

#include <iostream>
#include <sstream>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/traits.hpp>
#include <boost/serialization/level.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

using namespace boost::archive;
static constexpr unsigned int base_version = 1;

template<typename T>
struct Base : public boost::serialization::traits<Base<T>,
                                                 boost::serialization::object_class_info,
                                                 boost::serialization::track_never,
                                                 base_version> {
  T t;
  template<typename A>
  void load(A& a,
            unsigned int const in_version) {
    std::cerr << "Loading Base with version " << in_version  << std::endl;
    a & t;
  }
  template<typename A>
  void save(A& a,
            unsigned int const) const {
    std::cerr << "Saving Base with version " << base_version << std::endl;
    a & t;
  }
  BOOST_SERIALIZATION_SPLIT_MEMBER()
};

struct Derived : public Base<int> {
  int j;
  static constexpr unsigned int version = 2;
  template<typename A>
  void load(A& a,
            unsigned int const in_version) {
    std::cerr << "Loading Derived with version " << in_version << std::endl;
    a & boost::serialization::base_object<Base>(*this);
    a & j;
  }
  template<typename A>
  void save(A& a,
            unsigned int const) const {
    std::cerr << "Saving Derived with version " << version << std::endl;
    a & boost::serialization::base_object<Base>(*this);
    a & j;
  }
  BOOST_SERIALIZATION_SPLIT_MEMBER()
};
BOOST_CLASS_VERSION(Derived,Derived::version)

int main() {
  Derived d;
  d.t = 1;

  std::stringstream s;
  text_oarchive oa {s};
  oa << d;

  Derived e;
  text_iarchive ia{s};
  ia >> e;
  std::cerr << e.t << std::endl;

  return 0;
}

我得到的输出是:

Saving Derived with version 2
Saving Base with version 1
Loading Derived with version 2
Loading Base with version 2
1

似乎保存是用正确的版本完成的,而加载总是用派生类的版本完成的。这段代码有什么问题?

最佳答案

不是通过继承而是通过专门化 boost::serialization 命名空间的成员来定义 Base 的版本会导致我想要的行为:

// headers as before
#define BOOST_CLASS_TEMPLATE_VERSION(Template, Type, Version) \
namespace boost {                                           \
  namespace serialization {                                 \
    template<Template>                                      \
    struct version<Type> {                                  \
      static constexpr unsigned int value = Version;        \
    };                                                      \
    template<Template>                                      \
    constexpr unsigned int version<Type>::value;            \
  }                                                         \
}

using namespace boost::archive;
static constexpr unsigned int base_version = 1;

template<typename T>
struct Base {
  T t;
// ...
};
BOOST_CLASS_TEMPLATE_VERSION(typename T, Base<T>, base_version);
// rest as before

给予

$ ./main 
Saving Derived with version 2
Saving Base with version 1
Loading Derived with version 2
Loading Base with version 1
1

使用 Boost 1.67。

关于c++ - 从模板化类版本控制派生类的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58301881/

相关文章:

c++ - 如何在类对象中分配数组

c++ - 使 C++ 在模板函数的特定实例化时编译失败

django - 在 Django 中序列化一棵树

boost - 在 XCode 4.4 OS10.8 下找不到 <opencv2/opencv.hpp>

c++ - 未调用 Caffe Layer 函数

java - Postgresql - 如何在 Java 中将 bytea 转换为文本或字符串

java - Java中的序列化对象大小与内存对象大小

Windows 版本预处理器和为向后兼容而构建?

c++ - Boost::访问者运算符过载时的变体 "Error: no match for call to [...]"

c++ - 隐式转换的多参数