Serialization 对boost中抽象类序列化的引用

Serialization 对boost中抽象类序列化的引用,serialization,boost,reference-wrapper,Serialization,Boost,Reference Wrapper,我必须在抽象类的Boost向量(或任何其他容器)中序列化。 因为不可能直接创建抽象类的向量,所以我创建了这个抽象类的引用向量 但是如何在Boost std::reference_包装中序列化抽象类呢?当我试图直接这样做时,我得到一个错误,引用包装器的默认构造函数是不可访问的 提前谢谢 再仔细考虑一下,您可能不想序列化引用 由于逻辑上的引用是不可重置的,并且不“保存任何数据”,因此它们永远不会被“反序列化”(它们指向什么?临时对象?泄漏的堆对象?) 当然,reference\u包装器是可重新安装的

我必须在抽象类的Boost向量(或任何其他容器)中序列化。 因为不可能直接创建抽象类的向量,所以我创建了这个抽象类的引用向量

但是如何在Boost std::reference_包装中序列化抽象类呢?当我试图直接这样做时,我得到一个错误,引用包装器的默认构造函数是不可访问的


提前谢谢

再仔细考虑一下,您可能不想序列化引用

由于逻辑上的引用是不可重置的,并且不“保存任何数据”,因此它们永远不会被“反序列化”(它们指向什么?临时对象?泄漏的堆对象?)

当然,
reference\u包装器
是可重新安装的,但在这种情况下,您实际上只是将它们用作原始指针(可能拥有指针)

在这种情况下,只需将它们序列化为指针,并选择性地使用对象跟踪来防止重复(反)序列化

提示我认为您将受益于内置序列化支持的
boost::ptr_vector
。见下文(和)


演示 提出了一种简单的方法“教导”Boost序列化使用
reference\u wrapper
作为原始指针(这可能是您需要的):

/////////////////////////////////////////////////
// Allow serialization of `reference_wrapper`
namespace boost { namespace serialization {
    template<class Ar, class T> void save_construct_data(Ar & ar, std::reference_wrapper<T> const * t, unsigned) {
        T const * p = &t->get();
        ar << p;
    }
    template<class Ar, class T> void load_construct_data(Ar & ar, std::reference_wrapper<T> * t, unsigned) {
        T* p = nullptr;
        ar >> p;
        ::new(t) std::reference_wrapper<T>(*p);
    }

    template <class Ar, typename T> inline void serialize(Ar &, std::reference_wrapper<T>&, unsigned) { }
} }
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>

#include <boost/ptr_container/serialize_ptr_vector.hpp>
#include <functional>

using namespace boost;

/////////////////////////////////////////////////
// defining a virtual class hierarchy...
struct B {
    virtual ~B() = default;
    virtual std::ostream& print(std::ostream& os) const { return os << __PRETTY_FUNCTION__; }

  private:
    friend class boost::serialization::access;
    template <typename Ar> void serialize(Ar&, unsigned) const {
    }
};

struct D1 : B {
    virtual std::ostream& print(std::ostream& os) const { return os << _data; }

  private:
    std::string _data = "forty two";

    friend class boost::serialization::access;
    template <typename Ar> void serialize(Ar& ar, unsigned) {
        ar & boost::serialization::base_object<B>(*this);
        ar & _data;
    }
};

struct D2 : B {
    virtual std::ostream& print(std::ostream& os) const { return os << _data; }

  private:
    int _data = 42;

    friend class boost::serialization::access;
    template <typename Ar> void serialize(Ar& ar, unsigned) {
        ar & boost::serialization::base_object<B>(*this);
        ar & _data;
    }
};

BOOST_CLASS_EXPORT(B)
BOOST_CLASS_EXPORT(D1)
BOOST_CLASS_EXPORT(D2)

/////////////////////////////////////////////////
// sample data structure representing our program

struct Data {
    ptr_vector<B> storage;
    std::vector<B*> refvect;

    void fill_sample() {
        storage.push_back(new D1);
        storage.push_back(new D1);
        storage.push_back(new D2);
        storage.push_back(new D1);

        refvect.clear();
        for (auto it = storage.rbegin(); it != storage.rend(); ++it)
            refvect.push_back(&*it);
    }

    friend std::ostream& operator<<(std::ostream& os, Data const& data) {
        for (auto i : data.refvect) i->print(std::cout) << "\n";
        return os;
    }

    template <typename Ar> void serialize(Ar& ar, unsigned) {
        ar & storage & refvect;
    }
};

#include <sstream>
int main() {

    std::stringstream stream;
    {
        archive::binary_oarchive oa(stream);

        Data x;
        x.fill_sample();
        oa << x;

        std::cout << "Before:\n" << x << "\n";
    }

    {
        archive::binary_iarchive ia(stream);

        Data y;
        ia >> y;
        std::cout << "After:\n" << y << "\n";
    }

}
印刷品

Before:
forty two
42
forty two
forty two

After:
forty two
42
forty two
forty two

你想创建一个MCVE吗?不,什么是MCVE?如何创建它?我在谷歌上找不到任何关于它的信息。我试着做个时髦的人。创建一个