Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] How do I make boost::serializationwork with std::shared_ptr?
From: Robert Ramey (ramey_at_[hidden])
Date: 2013-09-29 16:48:04


Arman Schwarz wrote:
> Hi everyone, this is my first post so I apologies if I got some of
> the housekeeping wrong. I did my best.
>
> I also realise that this is likely to be a common question but all
> the responses I could find on Stackoverflow (among others) were
> related to older implementations of boost. My understanding is that
> Boost 1.54 is supposed to support `std::shared_ptr`.
>
> To illustrate the issue I am having, suppose we want to serialize a
> class containing a vector of shared pointers, along with a static
> `load` function that will build the class from a file and a `save`
> function that will store the instance to a file, and suppose that we
> can't just use the `serialize` member function for whatever reason
> (this particular example doesn't really illustrate such a case but in
> my code I can't use the `serialize` member function so this example
> doesn't use it either):

shared_pointers (boost and std) are not serializable as things stand.

For boost:shared_ptr I made special provision inside the archive
section of the serialization library. This breaks the fundamental
idea of the library which is the decoupling of archives from
serialization of data types. In other words, the library guarentees
that if one implements serialization for any data type, it will be
serializable by any archive class. This doesn't hold true for the
shared pointer data types. Due to the high place that shared_ptr
holds in the Boost hierarchy I felt I had to break a rule (decoupling
archive and serialization) in order to accomodate boost shared_ptr.

This was OK .... until now when we have a new shared_ptr to deal with.
The real solution is to enhance the serialization API so that the code
used for implementing boost::shared_ptr can be applied to other
data types which are otherwise unserializable. Unfortunately, this
is a non-trivial task which I have yet to get to. So for now I
can't offer a real solution other than suggesting one use boost
shared pointer rather than std::shared_ptr. I dont' know if this
helps - but there it is.

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net