Sleepless
Sleepless

Reputation: 103

C++ Serialization with Cereal error: Cereal does not support serializing raw pointers

I'm using the Cereal C++ serialization library (https://uscilab.github.io/cereal/index.html) to serialize some data.

The data types that I am trying to serialize mostly include vectors of vectors (e.g.: vector<vector<ushort>>) and basic types (e.g.: uint, vector<string>).

When I compile, I am getting the following error:

src/utils/cereal/types/common.hpp: In instantiation of ‘void cereal::serialize(Archive&, T*&) [with Archive = cereal::BinaryOutputArchive; T = Graph]’:
src/utils/cereal/cereal.hpp:408:39:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = Graph*; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_non_member_serialize<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_non_member_serialize<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/utils/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = Graph*; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/utils/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {Graph*}; ArchiveType = cereal::BinaryOutputArchive; unsigned int Flags = 1u]’
src/Graph.cpp:169:22:   required from here
src/utils/cereal/types/common.hpp:114:5: error: static assertion failed: Cereal does not support serializing raw pointers - please use a smart pointer
     static_assert(cereal::traits::detail::delay_static_assert<T>::value,

A Google search leads me to the following page regarding the error: https://uscilab.github.io/cereal/pointers.html

This page suggests the cause of the error is either from serializing "dumb" pointers, or serializing a type with no default constructor.

However, the Graph class that I am serializing has a default constructor Graph(), and I don't think I am serializing any "dumb" pointers.

Relevant Graph class code:

    Graph();
    vector<vector<ushort>> adjacencyList();
    vector<string> name();
    unordered_map<string, ushort> unorderedNodeNameMap();
    uint size;

    friend class cereal::access;

    template<class Archive>
    void serialize(Archive& archive)
    {
        archive(CEREAL_NVP(adjacencyList), CEREAL_NVP(name), CEREAL_NVP(unorderedNodeNameMap), CEREAL_NVP(size);
    }

Serialization:

void Graph::serializeGraph()
{
    ofstream ofs("b.out", ofstream::binary);
    {
        cereal::BinaryOutputArchive oArchive(ofs);
        oArchive(this);
    }
}

Upvotes: 0

Views: 974

Answers (1)

Sleepless
Sleepless

Reputation: 103

I forgot that this is a pointer type. I guess in addition to not being able to directly serialize "dumb" pointers, you also can't pass them to the output archive.

Passing the serializing object as a value solved it:

void Graph::serializeGraph(Graph a)
{
    ofstream ofs("b.out", ofstream::binary);
    {
        cereal::BinaryOutputArchive oArchive(ofs);
        oArchive(a);
    }
}

Upvotes: 0

Related Questions