hello, serializing a const char* is not allowed I gather? char* seems not to be allowed either. primitive types pointers are not serializable? I have a struct S{ double d; char c[SIZE]; const char* x; }; template <typename Archive> void serialize(Archive& ar, S& s, const unsigned int version) { ar & s.d; ar & s.c; ar & s.x; } the 3rd one seems to cause the compile error, removing constness didn't help. is one supposed to an array of char or a std::string instead regards,
Hi Hicham,
On Tue, Dec 7, 2010 at 6:24 PM, Hicham Mouline
serializing a const char* is not allowed I gather? char* seems not to be allowed either. primitive types pointers are not serializable?
Serializing *pointer values* makes little sense (what use would be the address, when you read it back in a different process?): you really want to serialize whatever object they are pointing to. But then, since pointers to non-class types are used in C to represent arrays, there's no safe way to know how many pointed values should be serialized, so Boost.Serialization errors out when you try to save them into an archive. There's an additional problem when serializing a variable-length array: you must split the `serialization` method into save + load: indeed, when you load a variable-len array from an archive, you must allocate a destination buffer large enough to hold all the values, which you don't when saving: the buffer is already there. The attached code provides an example. In the end: if you just want to serialize a string, use std::string and serialization will do it for you out-of-the-box. Best regards, Riccardo
On Dec 8, 2010, at 9:45 AM, Riccardo Murri wrote:
Hi Hicham,
On Tue, Dec 7, 2010 at 6:24 PM, Hicham Mouline
wrote: serializing a const char* is not allowed I gather? char* seems not to be allowed either. primitive types pointers are not serializable?
Indeed, primitive type pointers are not serializable. This is a design decision in Boost.Serialization. Unless you want to serialize a pointer to a single char read Riccardo's mail below. If you indeed need to send a pointer to a single char then you will need to wrap that char into a struct, and write a serialization function for it.
Serializing *pointer values* makes little sense (what use would be the address, when you read it back in a different process?): you really want to serialize whatever object they are pointing to. But then, since pointers to non-class types are used in C to represent arrays, there's no safe way to know how many pointed values should be serialized, so Boost.Serialization errors out when you try to save them into an archive.
There's an additional problem when serializing a variable-length array: you must split the `serialization` method into save + load: indeed, when you load a variable-len array from an archive, you must allocate a destination buffer large enough to hold all the values, which you don't when saving: the buffer is already there. The attached code provides an example.
In the end: if you just want to serialize a string, use std::string and serialization will do it for you out-of-the-box.
participants (3)
-
Hicham Mouline
-
Matthias Troyer
-
Riccardo Murri