USCiLab / cereal

A C++11 library for serialization
BSD 3-Clause "New" or "Revised" License
4.25k stars 767 forks source link

Compilation error with struct as smat_pointer #801

Open cshreyastech opened 1 year ago

cshreyastech commented 1 year ago

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&}; 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 = std::make_shared(); myStruct->intValue = 42;

to MyStruct myStruct; myStruct.intValue = 42.

Any suggestions on what I am missing?

Thanks