[serialization] serializing std::string*

It appears that std::string* is not serializable. I have need to serialize them, and I'm considering adding the old, unused code in boost/serialization/string.hpp (the stuff #if 0-ed out and marked with "left over from a previous incarnation - strings are now always primitive types") to serialize std::string*'s. I have 2 questions before doing so. 1) What is the rationale for making std::string a primitive, track_never type? After an extensive rereading of the docs, I'm left with only a hazy understanding of what the implications are for primitive vs. non-primitive status. 2) Can Robert (or anyone else) give me a sense for how likely I am to get myself into trouble doing this? If it matters, I don't need to be able to read old archives containing std::string's. Thanks, Zach Laine

Zach Laine wrote:
It appears that std::string* is not serializable.
I believe that it is serialzable - but it won't be tracked - which is probably not what you want.
I have need to serialize them, and I'm considering adding the old, unused code in boost/serialization/string.hpp (the stuff #if 0-ed out and marked with "left over from a previous incarnation - strings are now always primitive types") to serialize std::string*'s. I have 2 questions before doing so.
1) What is the rationale for making std::string a primitive, track_never type? After an extensive rereading of the docs, I'm left with only a hazy understanding of what the implications are for primitive vs. non-primitive status.
I used std::string in the archive header. In order to avoid a circular dependency, I made std::string primitive. I wasn't happy about this because it broke the identity between serialization::primitive and the normal usage of the phrase "prmitive type". But it was just too hard to do any other way. Maybe someday that might be looked at again.
2) Can Robert (or anyone else) give me a sense for how likely I am to get myself into trouble doing this? If it matters, I don't need to be able to read old archives containing std::string's.
I would recommend something else: class my_serializable_string : public std::string { .... }; ar << static_cast<my_serialzable_string *>(some_std_string); .... Robert Ramey
Thanks, Zach Laine _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Zach Laine wrote:
It appears that std::string* is not serializable.
I believe that it is serialzable - but it won't be tracked - which is probably not what you want.
FWIW, some_archive & some_std_string doesn't even compile with Boost 1.36 and 1.37, with GCC 4.1.0.
I have need to serialize them, and I'm considering adding the old, unused code in boost/serialization/string.hpp (the stuff #if 0-ed out and marked with "left over from a previous incarnation - strings are now always primitive types") to serialize std::string*'s. I have 2 questions before doing so.
1) What is the rationale for making std::string a primitive, track_never type? After an extensive rereading of the docs, I'm left with only a hazy understanding of what the implications are for primitive vs. non-primitive status.
I used std::string in the archive header. In order to avoid a circular dependency, I made std::string primitive. I wasn't happy about this because it broke the identity between serialization::primitive and the normal usage of the phrase "prmitive type". But it was just too hard to do any other way. Maybe someday that might be looked at again.
So, why didn't you do a trick like the one used below, and save a std::string-derived type instead of a std::string? Now users have to figure out this trick and presumably have to post to this list, since none of this is documented AFAICT.
2) Can Robert (or anyone else) give me a sense for how likely I am to get myself into trouble doing this? If it matters, I don't need to be able to read old archives containing std::string's.
I would recommend something else:
class my_serializable_string : public std::string { .... };
ar << static_cast<my_serialzable_string *>(some_std_string); ....
Well that works, so thanks! But a little note about this should really be in the docs, no? Zach

Zach Laine wrote:
Zach Laine wrote:
It appears that std::string* is not serializable.
I believe that it is serialzable - but it won't be tracked - which is probably not what you want.
FWIW, some_archive & some_std_string doesn't even compile with Boost 1.36 and 1.37, with GCC 4.1.0.
The serialization tests suite explicitly tests serialization of std::string on all platforms and compilers and archives and they all pass so maybe you're doing something wrong.
So, why didn't you do a trick like the one used below, and save a std::string-derived type instead of a std::string?
This trick can only be used to turn a primitive into a serializable type - not the other way around.
Now users have to figure out this trick and presumably have to post to this list, since none of this is documented AFAICT.
Maybe you should look for a better documented serialization library. If you want, you could submit a documentation patch as a TRAK item. Robert Ramey

On Wed, Dec 17, 2008 at 10:46 AM, Robert Ramey <ramey@rrsd.com> wrote:
Zach Laine wrote:
FWIW, some_archive & some_std_string doesn't even compile with Boost 1.36 and 1.37, with GCC 4.1.0.
The serialization tests suite explicitly tests serialization of std::string on all platforms and compilers and archives and they all pass so maybe you're doing something wrong.
Sorry, I meant "some_std_string_pointer".
So, why didn't you do a trick like the one used below, and save a std::string-derived type instead of a std::string?
This trick can only be used to turn a primitive into a serializable type - not the other way around.
I was referring to your statement that you need to serialize std::strings in the archive header. My point is just that it seems more appropriate to write some std::string-equivalent (apparently not using the trick you recommended to me, but perhaps something like a size S followed by S chars?) in the archive header, instead of forcing user code to treat std::string as a primitive type.
Now users have to figure out this trick and presumably have to post to this list, since none of this is documented AFAICT.
Maybe you should look for a better documented serialization library.
I'm just trying to make the one I use better, instead of starting from scratch.
If you want, you could submit a documentation patch as a TRAK item.
Will do. Zach
participants (2)
-
Robert Ramey
-
Zach Laine