1

我在这里有一个简单的课程:

template <typename T>
class clDaughter
{
public:

  T*        __pData;
  uint16_t  __u16Size;

  clDaughter() [...]

  ~clDaughter() [...]

  clDaughter(uint16_t const ku16Size)
  {
    this->__pData = (T*)malloc(ku16Size * sizeof(T));
    this->__u16Size = ku16Size;
  }

  template<class Archive>
  void save(Archive & ar) const
  {
    ar(this->__u16Size);
    ar(cereal::binary_data(this->__pData, this->__u16Size * sizeof(T)));
  }

  template<class Archive>
  void load(Archive & ar)
  {
    uint16_t u16LoadedSize;
    ar(u16LoadedSize);

    this->__pData = (T*)malloc(u16LoadedSize * sizeof(T));
    this->__u16Size = u16LoadedSize;
    ar(cereal::binary_data(this->__pData, this->__u16Size * sizeof(T)));
  }
};

这工作正常,我的意思是,序列化进出测试正常。

当我想在这里使用多态性时,麻烦就开始了。这个子类继承自一个纯虚拟母类,以及其他“类女儿”类。

class clDaugter_A : public clMother
{
  [...]
}
class clDaugter_B : public clMother
{
  [...]
}

而且,当我想使用CEREAL_REGISTER_TYPE(...)宏注册我的 clDaughter 类时,

CEREAL_REGISTER_TYPE(clDaugter_A<int>)   
CEREAL_REGISTER_TYPE(clDaugter_B<int>)

编译器崩溃

“谷物找不到提供的类型和存档组合的任何输出序列化函数”

看来问题确实来自这种binary_data(...)方法,因为如果我__pData在循环中序列化数组(丑陋的风格)

for (u16Idx = 0;..;..)
{
  ar(this->__pData[u16Idx];
}

我没有错误,它工作正常。只有当我binary_data()CEREAL_REGISTER_TYPE()一起使用时。

我错过了什么 ?

(为了先发制人,我确实想使用它,binary_data()因为它比循环快 20-30 倍,我需要在这里快速)

感谢您的帮助

4

0 回答 0