gpt4 book ai didi

c++ - boost std unique_ptr 的 STL 集合的序列化

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:58 24 4
gpt4 key购买 nike

我希望能够序列化 std::unique_ptr 的 STL 容器。可以吗?顺便说一句,单个 std::unique_ptr 一切正常。下面是我正在处理的代码,gcc 给出了以下错误:

 use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const
std::unique_ptr<_Tp, _Dp>&) [with _Tp = MyDegrees; _Dp =
std::default_delete<MyDegrees>; std::unique_ptr<_Tp, _Dp> =
std::unique_ptr<MyDegrees>]’

如何使代码正常工作?

#include <iostream>
#include <memory>
#include <fstream>
#include <map>
#include <vector>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
namespace boost {
namespace serialization {

template<class Archive, class T>
inline void save(
Archive & ar,
const std::unique_ptr< T > &t,
const unsigned int file_version
) {
// only the raw pointer has to be saved
const T * const tx = t.get();
//ar << tx;
ar << boost::serialization::make_nvp("px", tx);
}
template<class Archive, class T>
inline void load(
Archive & ar,
std::unique_ptr< T > &t,
const unsigned int file_version
) {
T *pTarget;
//ar >> pTarget;
ar >> boost::serialization::make_nvp("px", pTarget);

#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
t.release();
t = std::unique_ptr< T >(pTarget);
#else
t.reset(pTarget);
#endif
}
template<class Archive, class T>
inline void serialize(
Archive & ar,
std::unique_ptr< T > &t,
const unsigned int file_version
) {
boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost

class MyDegrees
{
public:
void setDeg(int d) {
deg = d;
}
int getDeg()const {
return deg;
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
//{ ar & deg; }
{
ar & boost::serialization::make_nvp("DEGS", deg);
}
int deg;
};
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
//{ ar & degrees; }
{
ar & boost::serialization::make_nvp("DEGS2", degrees);
ar & boost::serialization::make_nvp("DEGMAP", deg_map);
}
std::unique_ptr<MyDegrees> degrees;
std::vector<std::unique_ptr<MyDegrees> > deg_map;
public:
gps_position(): degrees(std::unique_ptr<MyDegrees>(new MyDegrees)) {};
void setDeg(int d) {
degrees->setDeg(d);
}
int getDeg() const {
return degrees->getDeg();
}
};

int TestBasicSerialize(int, char *[])
{
int numErr = 0;
double a;
std::ofstream ofs("filename");
gps_position g;
g.setDeg(45);
std::cout<<g.getDeg()<<std::endl;
{
boost::archive::text_oarchive oa(ofs);
oa << g;
}
//{ boost ::archive::xml_oarchive oa(ofs); oa << g;}
gps_position newg;
{
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
ia >> newg;
std::cout<<newg.getDeg()<<std::endl;
}
return numErr;

}

最佳答案

问题是容器反序列化器正在尝试复制构造一个 unique_ptr。为了演示,请考虑以下产生相同错误的代码:

std::vector< std::unique_ptr<int> > vec;
std::unique_ptr<int> p;
vec.push_back(p); // does not compile!

但是,这可以使用 std::move 解决:

vec.push_back(std::move(p)); // ok

最省力的解决方案是使用可复制构造的智能指针,例如 boost::shared_ptr,它带有它在 boost/serialization/shared_ptr.hpp 中的预定义序列化实现。


下一个解决方案是在类中手动序列化 vector :

//NOTE: this replaces void serialize(...) in gps_position
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
ar & BOOST_SERIALIZATION_NVP(degrees);
size_t size = deg_map.size();
ar & BOOST_SERIALIZATION_NVP(size);
for( auto it=deg_map.begin(), end=deg_map.end(); it!=end; ++it )
ar & boost::serialization::make_nvp("item",*it);
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(degrees);
size_t size = 0;
ar & BOOST_SERIALIZATION_NVP(size);
deg_map.clear();
deg_map.reserve(size);
while( size-- >= 0 ) {
std::unique_ptr<MyDegrees> p;
ar & boost::serialization::make_nvp("item",p);
deg_map.push_back(std::move(p));
}
}
BOOST_SERIALIZATION_SPLIT_MEMBER()

最后一个解决方案涉及编写您自己的容器序列化程序,就像您对 unique_ptr 所做的那样,它使用 std::move 添加项目:

namespace boost { namespace serialization {
//NOTE: do not include boost/serialization/vector.hpp
template<class Archive, class T, class Allocator>
inline void save(
Archive & ar,
const std::vector<T, Allocator> &t,
const unsigned int
){
collection_size_type count (t.size());
ar << BOOST_SERIALIZATION_NVP(count);
for(auto it=t.begin(), end=t.end(); it!=end; ++it)
ar << boost::serialization::make_nvp("item", (*it));
}

template<class Archive, class T, class Allocator>
inline void load(
Archive & ar,
std::vector<T, Allocator> &t,
const unsigned int
){
collection_size_type count;
ar >> BOOST_SERIALIZATION_NVP(count);
t.clear();
t.reserve(count);
while( count-- > 0 ) {
T i;
ar >> boost::serialization::make_nvp("item", i);
t.push_back(std::move(i)); // use std::move
}
}

template<class Archive, class T, class Allocator>
inline void serialize(
Archive & ar,
std::vector<T, Allocator> & t,
const unsigned int file_version
){
boost::serialization::split_free(ar, t, file_version);
}
} } // namespace boost::serialization

最后但同样重要的是,您应该使用:

oa << BOOST_SERIALIZATION_NVP(g);
// and
ia >> BOOST_SERIALIZATION_NVP(newg);

在使用 xml 存档时在 TestBasicSerialize() 中。

关于c++ - boost std unique_ptr 的 STL 集合的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13347776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com