
boost::serialization considers std::string a primative type. This means it causes problems sometimes when polymorphim or pointers are used. The solution is to make a wrapper around the string, something like: class tracked_string { public: tracked_string() {} tracked_string(const std::string& src) { m_string = src; } tracked_string(const tracked_string& src) : m_string(src) {} template<class Archive> void serialize(Archive &ar, unsigned int version) { ar & m_string; } // casting operators operator std::string() const { return m_string; } operator std::string() { return m_string; } private: std::string m_string; // the real data }; Michael Sperber wrote:
Hi there,
I'm trying to convert a piece of code from templated archives to polymorphic ones, and am running into trouble when serializing std::string. Either I get an error message saying that std::string has no serialize member function, or I get the attached message.
The relevant code is this:
---snip--- #include
#include
#include void mtime::serialize(boost::archive::polymorphic_iarchive& ar, const unsigned int file_version) { std::string iso; ar & boost::serialization::make_nvp("iso_time", iso); set_from_iso_string(iso); }
void mtime::serialize(boost::archive::polymorphic_oarchive& ar, const unsigned int file_version) { std::string iso =3D to_iso_string(); ar & boost::serialization::make_nvp("iso_time", iso); } ---snip---
Am I missing something obvious? Are certain types not directly supported for serialization via polymorphic archives? Any help would be much appreciated.