How can I serialize an std::vector with boost::serialization?

后端 未结 4 1108
星月不相逢
星月不相逢 2021-02-18 21:02
class workflow {

private:
friend class boost::serialization::access;

template
void serialize(Archive & ar, const unsigned int version)
{
  ar          


        
4条回答
  •  无人及你
    2021-02-18 21:39

    In case someone will ever need to write explicit 'serialize' method without any includes of boost headers (for abstract purposes, etc):

    std::vector dataVec;
    int size; //you have explicitly add vector size
    
    template 
    void serialize(Archive &ar, const unsigned int version)
    {
        if(Archive::is_loading::value) {
            ar & size;
            for(int i = 0; i < size; i++) {
                Data dat;
                ar & dat;
                dataVec.push_back(dat);
            }
        } else {
            size = dataVec.size();
            ar & size;
            for(int i = 0; i < size; i++) {
                ar & dataVec[i];
            }
        }
    }
    

提交回复
热议问题