Boost logo

Boost Users :

From: Jeffrey Holle (jeffreyholle_at_[hidden])
Date: 2006-02-22 11:35:07


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 <boost/serialization/serialization.hpp>
>
> #include <boost/archive/polymorphic_iarchive.hpp>
> #include <boost/archive/polymorphic_oarchive.hpp>
>
> 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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net