Using boost.serialization with STL containers as template parameters

boost-serialization, c++, stl

Solution

While serialiation/array.hpp has a `serialize()` overload for `boost::array`, it doesn't have one for `std::array`.

You should be able to provide your own `serialize()` overload using `make_array()` instead:

namespace boost {
namespace serialization {

template<class Archive, class T, size_t N>
void serialize(Archive & ar, std::array<T,N> & a, const unsigned int version)
{
  ar & boost::serialization::make_array(a.data(), a.size());
}

} // namespace serialization
} // namespace boost

Problem

I'd like to use boost.serialization with a templated container class: ``` // MyContainer.h template<class T> struct MyContainer { T t; template<class Archive> void serialize(Archive& archive, const unsigned version) { archive & t; } }; ``` When I use an STL container as template parameter, e.g. ``` // Main.cpp ... MyContainer<array<int,4>> mc; std::ofstream ofs("foo.bar"); boost::archive::binary_oarchive oa(ofs); oa << mc; ... ``` ...Visual Studio 11 complains with the following error message: ``` 'serialize' : is not a member of 'std::array<_Ty,_Size>' ``` I have tried to include "boost/serialization/array.hpp" in both files, but this didn't solve the problem. Besides, including the array specialization is not what I want, as the container may hold any other STL container as well. What is the correct way of doing this?

Original source