cereal icon indicating copy to clipboard operation
cereal copied to clipboard

Compilation error with struct as smat_pointer

Open cshreyastech opened this issue 2 years ago • 0 comments

Minimal code:

#include <cereal/archives/binary.hpp>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>

struct MyStruct {
  int intValue;

  // Serialization method using smart pointers
  template <class Archive>
  void serialize(Archive& archive) {
    archive(intValue);
  }
};

int main() {
  // Create an instance of MyStruct using std::shared_ptr
  std::shared_ptr<MyStruct> myStruct = std::make_shared<MyStruct>();
  myStruct->intValue = 42;

  // Serialize the std::shared_ptr to a binary file
  {
    std::ofstream ofs("data.bin", std::ios::binary);
    cereal::BinaryOutputArchive archive(ofs);
    archive(myStruct);
  }

  return 0;
}

Compilation error: cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {std::shared_ptr<MyStruct>&}; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1]’ [build] ../08-serialization/src/serialization_smart_ptr.cpp:31:21: required from here [build] ../external/cereal/include/cereal/cereal.hpp:570:87: error: static assertion failed: cereal could not find any output serialization functions for the provided type and archive combination.

This works on modify std::shared_ptr<MyStruct> myStruct = std::make_shared<MyStruct>(); myStruct->intValue = 42;

to MyStruct myStruct; myStruct.intValue = 42.

Any suggestions on what I am missing?

Thanks

cshreyastech avatar Sep 29 '23 02:09 cshreyastech