Serialization of boost::shared_ptr<int>?
Hi Robert and everyone,
Thank you for explaining to me about BOOST_STRONGTYPE. I'll let you
know how that goes for me. And thank you for being so patient and
trying to help me get serialization working in my program.
In the meantime, I'm experiencing a much simpler problem. Although I
can serialize classes of the form boost::shared_ptr<T>, where T is a
class I've defined, I get compilation errors if I try to serialize a
boost::shared_ptr<int>.
How can I resolve this?
Code snippet:
-----------------------------------------------
#include <iostream>
#include <fstream>
#include
Joseph schrieb:
In the meantime, I'm experiencing a much simpler problem. Although I can serialize classes of the form boost::shared_ptr<T>, where T is a class I've defined, I get compilation errors if I try to serialize a boost::shared_ptr<int>.
How can I resolve this?
In such error messages, the first thing to look at is where the actual error happens, indicated by gcc with "instantiated for here". You'll find it's a static assertion and along with it the comment: // The most common cause of trapping here would be serializing // something like shared_ptr<int>. This occurs because int // is never tracked by default. Wrap int in a trackable type As you can then look up in the documentation, for ints the address is not serialised, as their tracking traits default to track_never, which is probably sensible. Simply wrap you int in your own class, then you are fine. And this is as efficient. Jens
Try the following change - If this does it - read the rationale about using const with serialization. Robert Ramey Joseph Turian wrote:
Hi Robert and everyone,
Thank you for explaining to me about BOOST_STRONGTYPE. I'll let you know how that goes for me. And thank you for being so patient and trying to help me get serialization working in my program.
In the meantime, I'm experiencing a much simpler problem. Although I can serialize classes of the form boost::shared_ptr<T>, where T is a class I've defined, I get compilation errors if I try to serialize a boost::shared_ptr<int>.
How can I resolve this?
Code snippet: ----------------------------------------------- #include <iostream> #include <fstream>
#include
#include #include #include #include #include int main() { std::ofstream ofs("archive"); boost::archive::text_oarchive oa(ofs);
//boost::shared_ptr<int> i; const boost::shared_ptr<int> i; oa << i;
return 0; } -----------------------------------------------
Try the following change - If this does it - read the rationale about using const with serialization.
Unfortunately this doesn't change the compiler errors output at all. Can you duplicate this bad behavior on your system? Joseph -- http://www.cs.nyu.edu/~turian/
I compiled the following program on my system
#include <fstream>
#include
Try the following change - If this does it - read the rationale about using const with serialization.
Unfortunately this doesn't change the compiler errors output at all. Can you duplicate this bad behavior on your system?
Joseph
b) use BOOST_STRONG_TYPE to make an integer type that is tracked.
Okay. The code snippet you gave me worked great.
However, I am now experiencing what I hope is the final problem.
Basically, I have a recursive data structure A, which contains
pointers of type boost::smart_pointer<A>.
Could you please show me how to get the follow code snippet to work?
I should say that I'm really appreciative for all your help.
--------
#include <iostream>
#include <fstream>
#include
I can't see what you're trying to do here. The following would seem to be
equivalent to your intention
#include <fstream>
#include
I can't see what you're trying to do here.
The sample I provided is basically the simplest version of my code that, nonetheless, will not compile. In my code, it's much more complicated. Basically, I have class A, and class A has static member variables that are STL containers containing objects of type boost::shared_ptr<A>. But I tried to simplify it as much as possible to the basic piece which will not compile, which is what I provided in my last message. I suspect that if I could get the sample code (where A contains Aptr) working, then I'd be able to get my entire program serialized.
The following would seem to be equivalent to your intention
Unfortunately, I have to use smart_ptr's. Otherwise, managing object deletion will be extremely complicated.
I think you would find that spending some more time with the documentation and the examples would be a good investment.
I agree; I'm trying to understand the serialization library better so that I can answer my own questions. Nonetheless, if you are able to suggest a way to get serialization to work with class A containing static member variables of type boost::shared_ptr<A>, I'd be most appreciative. Thanks for your help so far. Best, Joseph -- http://www.cs.nyu.edu/~turian/
Joseph Turian wrote:
I can't see what you're trying to do here.
The sample I provided is basically the simplest version of my code that, nonetheless, will not compile.
In my code, it's much more complicated. Basically, I have class A, and class A has static member variables that are STL containers containing objects of type boost::shared_ptr<A>. But I tried to simplify it as much as possible to the basic piece which will not compile, which is what I provided in my last message.
I suspect that if I could get the sample code (where A contains Aptr) working, then I'd be able to get my entire program serialized.
Hmmm There are at least 3 totally orthogonal issues here. a) Serialization of static variables. This is handled the same as any other variables. Tracking can be used to ensure that only unique copies of the variables are serialized. b) Serialization of smart_ptr<int>. This would handled like seriliazation of another other type of smart pointer - except smart_ptr<T> can only be serialized if T is a tracked type. I showed how to use BOOST_STRONG_TYPE to make a type equivalent to an int which can be tracked and therefore be used as an argument to a serialized smart_ptr. c) Serializiation of contained pointers. This is common case handled by the library. The library considers and handles correctly the case where pointers are cyclic. I modified your code to show it compiles. (note there is also a test for cyclic pointers. I'm not aware of any other obstacles to doing what you want to do. Robert Ramey
There are at least 3 totally orthogonal issues here.
Yes, thank you for breaking it down. I believe we've isolated the trickiness to c)
a) Serialization of static variables. This is handled the same as any other variables. Tracking can be used to ensure that only unique copies of the variables are serialized.
I don't think this will be difficult for me to implement.
b) Serialization of smart_ptr<int>. This would handled like seriliazation of another other type of smart pointer - except smart_ptr<T> can only be serialized if T is a tracked type. I showed how to use BOOST_STRONG_TYPE to make a type equivalent to an int which can be tracked and therefore be used as an argument to a serialized smart_ptr.
Yes, thank you for explaining that for me.
c) Serializiation of contained pointers. This is common case handled by the library. The library considers and handles correctly the case where pointers are cyclic. I modified your code to show it compiles. (note there is also a test for cyclic pointers.)
Well, I see how your example compiles in the case of RAW cyclic pointers. However, I cannot use raw pointers, and I don't know how to get it to compile with cyclic smart pointers (specifically, boost::shared_ptr). Was there something that I missed? Thanks, Joseph -- http://www.cs.nyu.edu/~turian/
Well, I see how your example compiles in the case of RAW cyclic pointers. However, I cannot use raw pointers, and I don't know how to get it to compile with cyclic smart pointers (specifically, boost::shared_ptr). Was there something that I missed?
Just use a smart pointer instead of a raw pointer. The following
compiles as expected.
#include <fstream>
#include
Hey boost users,
I've hit what I figure is a compiler bug with release 1.33.1 under gcc
3.2, I'm trying to come up with a workaround.
I modified test_exported.cpp from the serialization test suite to
serialize via shared_ptr
troy d. straszheim wrote:
Which indicates that something has gone wrong with registration, it seems. One can toggle the buggy behavior by removing BOOST_IS_ABSTRACT(polymorphic_base). Whether polymorphic_base actually has a pure virtual function doesn't influence whether the error appears or not.
Note that "is_abstract" has had sort of history regarding serialization. Speaking from memory, boost::serialization::is_abstract punts to the official boost::is_abstract on platforms which support it and defaults to false on platforms that don't. This was arrived at in sort of an ad hoc fashion so it may be that there is something to be fixed there. Good Luck with this. Robert Ramey p.s. any chance of getting a business trip to the south pole out of this? RR
participants (4)
-
Joseph Turian
-
jth01@arcor.de
-
Robert Ramey
-
troy d. straszheim