提升序列化:未调用save_construct_data

时间:2015-07-06 21:53:05

标签: c++ boost boost-serialization

我在没有默认构造函数的情况下使用boost来序列化对象,但是我遇到了一个奇怪的问题:没有调用save_construct_data!

Bellow重现此问题的示例程序:

的main.cpp

#include <vector>
#include <iostream>
#include "Test.h"

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

int main()
{
    using T = float;
    walid::Test<T> instance ( 2, {2.3f, -0.5f} ) ;

    std::ofstream ofs ( "data" );
    boost::archive::text_oarchive oa ( ofs );    
    oa << instance;   
}

Test.h

#ifndef __Test_HEADER__
#define __Test_HEADER__

#include <list>
#include <vector>
#include <iostream>
#include <initializer_list>

namespace walid
{ 
    template<typename T>
    class Test
    {
      public:
        std::vector<T> elements;
    int in_dim ;

    Test(int input_dim, std::initializer_list<T> elem)
    {
      in_dim = input_dim;
      elements = std::vector<T>(elem);
    }
    void start(){};
    void stop(){};
    };
}

#include "Test_serialization.inl"

#endif

最后是Test_serialization.inl

namespace boost {
namespace serialization {

    template<class Archive, class T>
    inline void serialize(Archive & ar, walid::Test<T>& t, const unsigned int version)
    {
      std::cout<<"serialize Test ..."<<std::endl;
    }

    template<class Archive, class T>
    inline void save_construct_data ( Archive & ar, const walid::Test<T>* t, const unsigned int version )
    {
        std::cout<<"call save_construct_data Test ..."<<std::endl;

    }

    template<class Archive, class T>
    inline void load_construct_data ( Archive & ar, walid::Test<T>* t, const unsigned int version )
    {
        std::cout<<"call load_construct_data Test ..."<<std::endl;
        ::new ( t ) walid::Test<T> ( 2, {2.3f, -0.5f} ) ;
    }
}
}

这段代码应打印出来:

serialize Test ... 
call save_construct_data Test ...

但它只打印serialize Test ...(不调用save_construct_data)

我错过了什么吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

假设您正在构建walid::Test<T>

但是,如果仔细观察,你会发现自己并没有真正构建任何东西。

按设计Boost Serialization(de)序列化(转换) lvalues 。在反序列化期间唯一需要构造的地方是序列化动态分配的对象。

您可以通过将实例的存储更改为例如来说服自己。 shared_ptr

<强> Live On Coliru

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

#include <boost/shared_ptr.hpp>

int main()
{
    using T = float;
    using Test = walid::Test<T>;

    {
        boost::shared_ptr<Test> shared_instance(new Test(2, {2.3f, -0.5f}));

        std::ofstream ofs("data");
        boost::archive::text_oarchive oa(ofs);
        oa << shared_instance;
    }

    {
        boost::shared_ptr<Test> deserialized;

        std::ifstream ifs("data");
        boost::archive::text_iarchive ia(ifs);
        ia >> deserialized;
    }
}

打印

call save_construct_data Test ...
serialize Test ...
call load_construct_data Test ...
serialize Test ...