Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation error with struct as smat_pointer #801

Open
cshreyastech opened this issue Sep 29, 2023 · 0 comments
Open

Compilation error with struct as smat_pointer #801

cshreyastech opened this issue Sep 29, 2023 · 0 comments

Comments

@cshreyastech
Copy link

cshreyastech commented Sep 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant