Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cereal does not support raw pointers

Edit: The question title was based on a deep missunderstanding of the compiler error I got. I was (sillyly) assuming the error was, that I tried to deserialize to an object I declared inside of the function. This was utterly wrong. I didn't do enough debug efforts myself or I could have found out what was wrong. So the title was quite missleading and I changed it. Thanks to Андрей Беньковский for helping.


I'm writing Serialization functions for 3D Models in my engine using cereal, which proves to be really efficient and easy to use. So far everything worked great when I tested (de-)serializing a simple Mesh. But now I'm trying to deserialize another class but ran in a problem I don't get.

void loadFile(std::string filepath)
{
    DescriptionFile file;

    {
        ifstream stream = ifstream(filepath, std::ifstream::binary);
        PortableBinaryInputArchive archive(stream);
        archive(file);
        stream.close();
    }
}

This is my class, that should be deserialized:

struct DescriptionFile 
{
public:
    DescriptionFile(){}

    map<string, MeshDescription*> meshDescriptions;
    map<string, ModelDescription*> modelDescriptions;

public:
    template<class Archive>
    void serialize(Archive & archive)
    {
        archive(meshDescriptions, modelDescriptions);
    }
};

It gives me compiler error: Cereal does not support serializing raw pointers - please use a smart pointer Even though it's not a pointer. In another part of the code something similar works just fine. I'd be glad if somebody could help me solve this.

like image 265
LukeG Avatar asked Oct 18 '22 23:10

LukeG


1 Answers

I never used Cereal, but it looks like it expects you to use something like this:

map<string, unique_ptr<MeshDescription> >

To get std::unique_ptr I usually #include <memory>

From cereal documentation:

cereal supports serializing smart pointers but not dumb pointers (that is to say raw pointers, such as int *) or references. Pointer support can be found by including <cereal/types/memory.hpp>.

May be it means you have to include their header instead of the standard.

P.S. When your object owns resources (e.g. dynamically allocated MeshDescription) always delegate resource management (allocation, deallocation, copying, etc) to a separate class (smart pointer, collection, wrapper, etc). See rule of 0.

like image 146
Андрей Беньковский Avatar answered Oct 30 '22 02:10

Андрей Беньковский