Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-05-26 09:05:58

From: <williamkempf_at_[hidden]>

> > OK. Do you agree that it is a real problem? How would you solve it?
> (I mean
> > the problem with serializing a shared_ptr.)
> Using the same techniques I'd use if raw pointers were being used
> instead.

Raw pointers can't be serialized in the general case. You don't know whether
the pointer owns the object.

> Seriously, the example given is a very non-trivial example
> and knowing if an object has been previously serialized is not going
> to be enough info to persist the containing object any way.

It's enough info for me:

int last_id = 0;
std::map<shared_ptr_id, int> m;

void write(Storage & s, shared_ptr<T> const & p)
    std::map<shared_ptr_id, int>::iterator i = m.find(;

    if(i != m.end())
        write(s, i->second);
        m[] = last;
        write(s, last);
        // use typeid(*p) to find the appropriate serialization function
        // serialize *p

Peter Dimov
Multi Media Ltd.

Boost list run by bdawes at, gregod at, cpdaniel at, john at