sunmat
sunmat

Reputation: 7268

boost::container::vector cannot be serialized with Cereal?

I'm trying to serialize a boost::container::vector<int> using Cereal (I know boost provides a serialization library similar to Cereal but the whole project uses Cereal and there is just one corner of it that happens to rely on a boost vector).

I have defined templated save and load functions for boost::container::vector<int>, yet Cereal fails with a static assert saying it can't find a save/load pair of function, a serialize function, or a save_minimal/load_minimal pair of functions.

Here is a simple code that showcases this problem. For comparison, I have also defined a template structure myStruct that takes the same kind of template arguments as a boost::container::vector, to check that the same code does work with my own types.

#include <cereal/archives/binary.hpp>
#include <sstream>
#include <boost/container/vector.hpp>

namespace bc = boost::container;

template<typename T, typename V = void, typename W = void>
struct myStruct {};

template<typename A>
void save(A& ar, const myStruct<int>& v) {}

template<typename A>
void load(A& ar, myStruct<int>& v) {}

template<typename A>
void save(A& ar, const bc::vector<int>& v) {}

template<typename A>
void load(A& ar, bc::vector<int>& v) {}

int main()
{
    std::stringstream ss;
    {
        cereal::BinaryOutputArchive oarchive(ss);
        bc::vector<int> myData;
        //myStruct<int> myData;
        oarchive(myData); 
    }
    {
        cereal::BinaryInputArchive iarchive(ss); 
        bc::vector<int> myData;
        //myStruct<int> myData;
        iarchive(myData); 
    }
}

EDIT: note that I also tried to redefine CEREAL_SERIALIZE_FUNCTION_NAME, CEREAL_LOAD_FUNCTION_NAME, and CEREAL_SAVE_FUNCTION_NAME before including any cereal headers, as I though save and load methods may be conflicting with boost-provided functions, but even with renamed functions it doesn't work.

Upvotes: 1

Views: 161

Answers (1)

Armut
Armut

Reputation: 1137

You have to define the save/load functions within namespace cereal or namespace boost::container.

Upvotes: 0

Related Questions