Question

This question has been asked here and a few other places but the answers don't really seem to address the latest Boost library.

To illustrate the issue, suppose we want to serialize a class containing a shared pointer (std::shared_ptr), 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:

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

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

class A
{
public:
    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) 
        {
            ar & v;
        }
};

// 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;
}

int main()
{

}

The above code generates a long list of errors starting with c:\local\boost_1_54_0\boost\serialization\access.hpp(118): error C2039: 'serialize' : is not a member of 'std::shared_ptr<_Ty>', which as far as I understand shouldn't be true given that I have loaded the boost shared_ptr serialization library which ostensibly supports std::shared_ptr. What am I missing here?

NOTE: As far as I understand, my assumption that boost/serialization/shared_ptr.hpp defined a serialize function for std::shared_ptr was wrong, hence the correct answer to this question is probably that I'd either have to define my own serialize functions for std::shared_ptr or convert to boost::shared_ptr

Was it helpful?

Solution 2

No, std::shared_ptr and boost::shared_ptr are unrelated class templates.

Boost.Serizalization doesn't support std::shared_ptr out of the box, but you can add such a support in your application - just take a look at <boost/serialization/shared_ptr.hpp> header.

OTHER TIPS

This is the best answer I have been able to come up with myself. If someone has something better to say on this I will accept that as an answer instead.

The boost/serialization/shared_ptr.hpp header that boost ships with is not support for std::shared_ptr but for boost::shared_ptr. If you want to make serialization work with shared pointer objects without bootlegging your own serialization code then you will need to convert your std::shared_ptr objects to boost::shared_ptr objects and live with the consequences.

My misunderstanding was that I thought that boost/serialization/shared_ptr.hpp defined a serialize method for std::shared_ptr. I was wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top