从模板化类版本控制序列化派生类
Serialization of derived class from templated class 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
似乎保存是使用正确的版本完成的,而加载始终是使用派生类的版本完成的。这段代码有什么问题?
定义 Base 的版本不是通过继承而是通过专门化boost::serialization
命名空间的成员来定义,这会导致我想要的行为:
// 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
与提升 1.67。
相关文章:
- C++11 谷物序列化 - 版本控制
- Msgpack中是否具有版本控制功能
- 从模板化类版本控制序列化派生类
- 哪些 Netbeans 8.2 项目文件应提交到C++项目的版本控制?
- 基于布尔标志有效地派生公共函数的两个版本
- 如何创建受版本控制的 DLL
- 如何在性能和VMS上对C++进行代码审查/调试/编码/测试/版本控制
- GCC 函数多版本控制和命名空间
- 如何使用 c++/QT 在控制面板中更新应用程序版本auto_update
- 如何在编译C++代码时解决一些版本控制问题
- C++STL版本控制问题+Boost
- 避免DLL版本控制问题
- 如何以编程方式更改控制面板中显示的项目产品版本
- 派生类是否需要在基类上声明自己的虚函数版本
- VC++VS2013项目从非版本控制文件夹移动到版本控制文件夹后未进行编译
- 如何让编译器为派生类选择方法的非模板版本
- 这些辅助文件是否应在 Git 版本控制下
- C++命名空间版本控制
- 不执行派生类版本的虚函数
- 向静态库中添加一个全局变量以进行版本控制