|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-08-21 16:22:24
Hughes, James:
>> You are the second person to cite an example of serializing
>> shared_ptr. The question is about raw pointers.
>
> Sorry, been using smart pointers for too long, and forgotten there are
> those other ones....but surely the same principles apply?
The difference between a raw pointer and a smart pointer is that the type of
the smart pointer gives you enough context to let you decide what objects to
track, if any.
auto_ptr<X> and unique_ptr<X> for example need no tracking.
Reference-counted smart pointers such as shared_ptr<X> and intrusive_ptr<X>
need only track their targets, not all objects of type X. They also don't
have to guess whether a type needs tracking or not; if one serializes a
shared_ptr<int>, that int must be tracked. (The full capabilities of
shared_ptr make things much more interesting, but even the simplistic
approach that assumes no user-defined deleters covers the majority of the
use cases.)
A raw pointer carries no extra semantic information and can point to
literally _anything_:
- An object that has been serialized;
- ... including an element of a collection that has been serialized;
- ... including a subobject;
- A persistent object that is not part of the archive;
- ... including a static object;
- ... including an element of a collection initialized on startup;
- ... including a function;
- An array element;
- ... or past the end of the last array element;
- An object on the heap that is not owned by this pointer;
- An object on the heap that is owned by this pointer;
and so on.
The serialization library tries to cover some of the most common uses.
Whether supporting the last bullet is a feature or a misfeature is a matter
of opinion; I personally would never need this capability in user code
(except as an implementation detail in the serialization of intrusive_ptr.)
On the other hand, I've encountered several uses for the ability to
serialize a pointer to a static object or a function, something that
boost::serialization doesn't currently support.
I'm not sure what the "best" serialization system must do when handed a
pointer, but it's certainly possible to design a value-based system that
does no tracking on its own.
Robert Ramey:
> Hmmm - it wouldn't occur to me that one could serialize
> a shared_ptr without the ability to serialize a raw pointer.
Correct shared_ptr serialization requires a duplicate tracking
infrastructure anyway; it's possible to partially reuse the built-in
tracking, but it's also possible to not use it at all.
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