Boost logo

Boost Users :

Subject: [Boost-users] [Serialization] How do I make boost::serialization work with std::shared_ptr?
From: Arman Schwarz (armanschwarz_at_[hidden])
Date: 2013-09-28 04:08:41


Hi everyone, this is my first post so I apologies if I got some of the
housekeeping wrong. I did my best.

I also realise that this is likely to be a common question but all the
responses I could find on Stackoverflow (among others) were related to
older implementations of boost. My understanding is that Boost 1.54 is
supposed to support `std::shared_ptr`.

To illustrate the issue I am having, suppose we want to serialize a class
containing a vector of shared pointers, along with a static `load` function
that will build the class from a file and a `save` function that will store
the instance to a file, and suppose that we can't just use the `serialize`
member function for whatever reason (this particular example doesn't really
illustrate such a case but in my code I can't use the `serialize` member
function so this example doesn't use it either):

    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>

    #include <boost/serialization/vector.hpp>
    #include <boost/serialization/shared_ptr.hpp>

    #include <fstream>
    #include <memory>
    #include <vector>

    class A
    {
    public:
        std::vector<std::shared_ptr<int>> v;

        void A::Save(char * const filename);
        static A * const Load(char * const filename);

            //////////////////////////////////
            // Boost Serialization:
            //
        private:
            friend class boost::serialization::access;
            template<class Archive> void serialize(Archive & ar, const
unsigned int file_version) {}
    };

    // save the world to a file:
    void A::Save(char * const filename)
    {
        // create and open a character archive for output
        std::ofstream ofs(filename);

        // save data to archive
        {
            boost::archive::text_oarchive oa(ofs);

            // write the pointer to file
            oa << this;
        }
    }

    // load world from file
    A * const A::Load(char * const filename)
    {
        A * a;

        // create and open an archive for input
        std::ifstream ifs(filename);

        boost::archive::text_iarchive ia(ifs);

        // read class pointer from archive
        ia >> a;

        return a;
    }

    namespace boost
    {
        namespace serialization
        {
            template<class Archive>
            inline void save_construct_data(
            Archive & ar, A const * t, unsigned const int file_version
            )
            {
                ar << t->v;
            }

            template<class Archive>
            inline void load_construct_data(
            Archive & ar, A * t, const unsigned int file_version
            )
            {
                ar >> t->v;
            }
        }
    }

    int main()
    {

    }

Why doesn't this code compile? What am I doing wrong here? The error
message states that 'serialize' is not a member of 'shared_ptr'. Why am I
am getting this message?



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