How to use boost::serialization for std::stack?

430 Views Asked by At

I know we can use boost::serialization for std::vector , list ,map etc as long as I include the corresponding boost header file like:

#include <boost/serialization/list.hpp>

But how could I use boost::serialization for std::stack ?

Thanks

2

There are 2 best solutions below

0
On

Replace std::stack with std::deque and include:

#include <boost/serialization/deque.hpp>
0
On

I know this is old, but I recently came across this problem. I was not satisfied with the solution of using std::deque because I really needed only a stack interface. Currently, boost support serialization of std::stack this by including these header files in this order

#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

A complete example would look something like:

#include <string>
#include <stack>
#include "boost/archive/text_oarchive.hpp"
#include "boost/archive/text_iarchive.hpp"
#include "boost/serialization/deque.hpp"
#include "boost/serialization/stack.hpp"

struct s
{
    std::stack<std::string> stack;

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