serializing classes using boost serialization with

2019-09-01 04:09发布

问题:

this piece of code has to be written every time we make a class i.e.from template<class archive> to ar & BOOST_SERIALIZATION_NVP(b) . How can we make it short? and how can we serialize the stl containers ?

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

    int a; 
    int b; 

public:  
    Employee(int a, int b)  
    { 
         this->a = a; 
         this->b = b; 
    } 

}; 

回答1:

I suggest you start with the documentation :)

  • http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html (start under Serializable Concept)

STL containers are serializable when you include the relevant header:

#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>

A host of other stuff is supported out of the box.

You could make generic wrappers for types that have already been made "reflectible" by other means (e.g. Fusion Sequences, Qt QObjects etc.)



标签: c++ boost