1

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

Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211

2 Answers2

2

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.

Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211
  • Conversion from `std::shared_ptr` to `boost::shared_ptr` I've seen stores as a destructor a mutable lambda that captures by value `std::shared_ptr`. So if you want to serialize that kind of structure, you would still need a way to serialize the destructor, and, hence the `std::shared_ptr`. So its the chicken and eggs problem. Are you aware of other conversions that do it differently? – iggy Nov 09 '15 at 19:17
1

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.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • I am aware of that; it's the basis for my question. – quant Sep 28 '13 at 12:15
  • @ausairman Then your question is unclear. You state that "Boost 1.54, <...> is supposed to support std::shared_ptr" - which is incorrect. – Igor R. Sep 28 '13 at 16:01
  • Ok. I have now explicitly stated that I am attempting to use the `boost/serialization/shared_ptr.hpp` header. – quant Sep 28 '13 at 22:45
  • Also, my understanding is that it *does* support `std::shared_ptr`; why else would it ship with a header that defines serialization methods for it? – quant Sep 28 '13 at 22:48
  • Sorry, I think I misunderstood the use of that library and your answer. Are you saying that the `boost/serialization/shared_ptr.hpp` header is *not* a serialization for `std::shared_ptr`? – quant Sep 29 '13 at 00:43
  • @ausairman Correct. `boost/serialization/shared_ptr.hpp` implements non-intrusive serialization for `boost::shared_ptr`. There's no support for `std::shared_ptr`, but one can implement it for his own needs, it's quite simple. – Igor R. Sep 29 '13 at 04:31