Boost Serialization - Serialize std::tr1::shared_ptr?
Asked Answered
S

1

2

Boost::Serialization has builtin support for boost::shared_ptr<>.

Is there a way to use this support for std::tr1::shared_ptr<> too?
Is it possible to cast from std::tr1::shared_ptr<> to boost::shared_ptr<>?

Susannahsusanne answered 4/11, 2010 at 7:21 Comment(3)
If you can use boost anyway, why don't you just use boost::shared_ptr in the first place ?Genitourinary
Cause the interface is boost-independent. Using boost::serialization for serializing the classes is just an implementation-detail. If later another developer decides to use another implementation for serialization, the project would still be boost-dependant.Susannahsusanne
Why don't just use typedef boost::shared_ptr MySharedPtr, so your code factically would be not boost-dependent, but typedef-dependent. You could change both serialization library and typedefs in the future.Touslesmois
A
3

A casting will not be possible as implementations differ. Also creating an instance of the one shared_ptr type with the value returned from get() on the other shared_ptr type will not work correctly as the reference countings will go to 0 at different points in your code which leads to deletion of the object before the last use of it. I am not an expert of boost::serialization but as the interfaces of std::tr1::shared_ptr<> and boost::shared_ptr<> are nearly identical it is very likely that you can just clone the serialization/deserialization code of boost::shared_ptr<> and only have to replace the namespace of the shared_ptr.

Antiserum answered 4/11, 2010 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.