gpt4 book ai didi

c++ - 使用模板类 boost 序列化

转载 作者:搜寻专家 更新时间:2023-10-31 01:03:18 24 4
gpt4 key购买 nike

我正在为 Linux 中的现有 C++ 项目添加 boost 序列化支持。在该项目中,有一个名为 ContainerT 的模板类,MString 是通过使用模板类型作为 char 从该类派生的。

Person类中使用了MString类型,想在Person类中序列化数据,Person类如下(由于实际项目较大,这里只给出了一部分)

#ifndef PERSON_H_
#define PERSON_H_

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <string>
#include <map>
#include "Container.h"
class Person
{
public:
Person();
Person(std::string _zName,int _iAge);
~Person();

private:
std::string z_Name;
int i_Age;
MString z_CountryCode;


//Serailization
//Allow serialization to access non-public data members.
friend class boost::serialization::access;

template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & z_Name;
ar & i_Age;
ar & z_CountryCode;
}
};


#endif /* PERSON_H_ */

模板类如下。

#ifndef CONTAINER_H_
#define CONTAINER_H_


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

template<typename T>
class ContainerT;

template<typename T>
class ContainerDataT
{
public:
ContainerDataT();
~ContainerDataT(){};

T* GetData(){return p_Data;};

private:
friend class ContainerT<T>;

T p_Data[];

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version)
{
ar & p_Data;
}
};

template<typename T>
class ContainerT
{
public:

ContainerT();
ContainerT(const T* _zData);
~ContainerT();
const T* GetMString() const;

private:

ContainerDataT<T>* p_Data;


friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version)
{
ar & p_Data;
}
};

template<typename T>
ContainerT<T>::ContainerT()
{
p_Data = (ContainerDataT<T>*)malloc(1);
p_Data->GetData()[0] = 0;
}


template<typename T>
ContainerT<T>::ContainerT(const T* _zData)
{
const T* pChar = _zData;

while(*pChar != 0)
++pChar;
int iLength = pChar - _zData;

p_Data = (ContainerDataT<T>*)malloc(500);
memcpy(p_Data->GetData(), _zData, iLength * sizeof(T));
p_Data->GetData()[iLength] = 0;
}

template<typename T>
ContainerT<T>::~ContainerT()
{
free(p_Data);
}

template<typename T>
const T* ContainerT<T>::GetMString() const
{
return p_Data->GetData();
}


typedef ContainerT<char> MString;

#endif /* CONTAINER_H_ */

当我编译代码时,当我尝试序列化 MString 时出现以下错误。如果我评论了 ar & z_CountryCode; Person 类中的一行一切正常。有人可以帮助我确定问题所在。有什么特殊的方法可以序列化模板类吗?

/usr/include/boost/type_traits/is_abstract.hpp: In instantiation of ‘boost::detail::is_abstract_imp2<ContainerDataT<char> >’:
/usr/include/boost/type_traits/is_abstract.hpp:121: instantiated from ‘const bool boost::detail::is_abstract_imp<ContainerDataT<char> >::value’
/usr/include/boost/type_traits/is_abstract.hpp:128: instantiated from ‘boost::is_abstract<ContainerDataT<char> >’
/usr/include/boost/serialization/is_abstract.hpp:32: instantiated from ‘boost::serialization::is_abstract<ContainerDataT<char> >’
/usr/include/boost/mpl/if.hpp:67: instantiated from ‘boost::mpl::if_<boost::serialization::is_abstract<ContainerDataT<char> >, boost::mpl::identity<boost::archive::detail::save_pointer_type<boost::archive::text_oarchive, ContainerDataT<char>*>::abstract<ContainerDataT<char> > >, boost::mpl::identity<boost::archive::detail::save_pointer_type<boost::archive::text_oarchive, ContainerDataT<char>*>::non_abstract<ContainerDataT<char> > > >’
/usr/include/boost/mpl/eval_if.hpp:37: instantiated from ‘boost::mpl::eval_if<boost::serialization::is_abstract<ContainerDataT<char> >, boost::mpl::identity<boost::archive::detail::save_pointer_type<boost::archive::text_oarchive, ContainerDataT<char>*>::abstract<ContainerDataT<char> > >, boost::mpl::identity<boost::archive::detail::save_pointer_type<boost::archive::text_oarchive, ContainerDataT<char>*>::non_abstract<ContainerDataT<char> > > >’
/usr/include/boost/archive/detail/oserializer.hpp:359: instantiated from ‘static const boost::archive::detail::basic_pointer_oserializer* boost::archive::detail::save_pointer_type<Archive, TPtr>::register_type(Archive&, T&) [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive, TPtr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/oserializer.hpp:460: instantiated from ‘static void boost::archive::detail::save_pointer_type<Archive, TPtr>::invoke(Archive&, TPtr) [with Archive = boost::archive::text_oarchive, TPtr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/oserializer.hpp:536: instantiated from ‘void boost::archive::save(Archive&, const T&) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>*]’
/usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from ‘void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = ContainerDataT<char>* const, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = ContainerDataT<char>* const, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:86: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator&(T&) [with T = ContainerDataT<char>*, Archive = boost::archive::text_oarchive]’
Container.h:52: instantiated from ‘void ContainerT<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char]’
/usr/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/archive/detail/oserializer.hpp:148: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
Main.cpp:110: instantiated from here
/usr/include/boost/type_traits/is_abstract.hpp:81: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
/usr/include/boost/archive/detail/oserializer.hpp: In static member function ‘static void boost::archive::detail::save_non_pointer_type<Archive, T>::invoke(Archive&, const T&) [with Archive = boost::archive::text_oarchive, T = char [0u]]’:
/usr/include/boost/archive/detail/oserializer.hpp:536: instantiated from ‘void boost::archive::save(Archive&, const T&) [with Archive = boost::archive::text_oarchive, T = char [0u]]’
/usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from ‘void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = const char [0u], Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = const char [0u], Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:86: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator&(T&) [with T = char [0u], Archive = boost::archive::text_oarchive]’
Container.h:29: instantiated from ‘void ContainerDataT<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char]’
/usr/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>]’
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>]’
/usr/include/boost/archive/detail/oserializer.hpp:226: instantiated from ‘boost::archive::detail::pointer_oserializer<T, Archive>::pointer_oserializer() [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/oserializer.hpp:198: instantiated from ‘const boost::archive::detail::pointer_oserializer<ContainerDataT<char>, boost::archive::text_oarchive> boost::archive::detail::pointer_oserializer<ContainerDataT<char>, boost::archive::text_oarchive>::instance’
/usr/include/boost/archive/detail/oserializer.hpp:190: instantiated from ‘static const boost::archive::detail::pointer_oserializer<T, Archive>& boost::archive::detail::pointer_oserializer<T, Archive>::instantiate() [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/oserializer.hpp:515: instantiated from ‘const boost::archive::detail::basic_pointer_oserializer& boost::archive::detail::instantiate_pointer_oserializer(Archive*, T*) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:58: instantiated from ‘const boost::archive::detail::basic_pointer_oserializer* boost::archive::detail::interface_oarchive<Archive>::register_type(const T*) [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/oserializer.hpp:344: instantiated from ‘static const boost::archive::detail::basic_pointer_oserializer* boost::archive::detail::save_pointer_type<Archive, TPtr>::non_abstract<T>::register_type(Archive&) [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive, TPtr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/oserializer.hpp:360: instantiated from ‘static const boost::archive::detail::basic_pointer_oserializer* boost::archive::detail::save_pointer_type<Archive, TPtr>::register_type(Archive&, T&) [with T = ContainerDataT<char>, Archive = boost::archive::text_oarchive, TPtr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/oserializer.hpp:460: instantiated from ‘static void boost::archive::detail::save_pointer_type<Archive, TPtr>::invoke(Archive&, TPtr) [with Archive = boost::archive::text_oarchive, TPtr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/oserializer.hpp:536: instantiated from ‘void boost::archive::save(Archive&, const T&) [with Archive = boost::archive::text_oarchive, T = ContainerDataT<char>*]’
/usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from ‘void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = ContainerDataT<char>* const, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = ContainerDataT<char>* const, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:86: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator&(T&) [with T = ContainerDataT<char>*, Archive = boost::archive::text_oarchive]’
Container.h:52: instantiated from ‘void ContainerT<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char]’
/usr/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
/usr/include/boost/archive/detail/oserializer.hpp:148: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = ContainerT<char>]’
Main.cpp:110: instantiated from here
/usr/include/boost/archive/detail/oserializer.hpp:316: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
/usr/include/boost/archive/detail/iserializer.hpp: In static member function ‘static void boost::archive::detail::load_non_pointer_type<Archive, T>::invoke(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = char [0u]]’:
/usr/include/boost/archive/detail/iserializer.hpp:572: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = char [0u]]’
/usr/include/boost/archive/basic_text_iarchive.hpp:64: instantiated from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = char [0u], Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/text_iarchive.hpp:64: instantiated from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = char [0u], Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:76: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = char [0u], Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:83: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = char [0u], Archive = boost::archive::text_iarchive]’
Container.h:29: instantiated from ‘void ContainerDataT<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive, T = char]’
/usr/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerDataT<char>]’
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerDataT<char>]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerDataT<char>]’
/usr/include/boost/archive/detail/iserializer.hpp:332: instantiated from ‘boost::archive::detail::pointer_iserializer<T, Archive>::pointer_iserializer() [with T = ContainerDataT<char>, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/iserializer.hpp:211: instantiated from ‘const boost::archive::detail::pointer_iserializer<ContainerDataT<char>, boost::archive::text_iarchive> boost::archive::detail::pointer_iserializer<ContainerDataT<char>, boost::archive::text_iarchive>::instance’
/usr/include/boost/archive/detail/iserializer.hpp:203: instantiated from ‘static const boost::archive::detail::pointer_iserializer<T, Archive>& boost::archive::detail::pointer_iserializer<T, Archive>::instantiate() [with T = ContainerDataT<char>, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/iserializer.hpp:546: instantiated from ‘const boost::archive::detail::basic_pointer_iserializer& boost::archive::detail::instantiate_pointer_iserializer(Archive*, T*) [with Archive = boost::archive::text_iarchive, T = ContainerDataT<char>]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:57: instantiated from ‘const boost::archive::detail::basic_pointer_iserializer* boost::archive::detail::interface_iarchive<Archive>::register_type(T*) [with T = ContainerDataT<char>, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/iserializer.hpp:449: instantiated from ‘static const boost::archive::detail::basic_pointer_iserializer* boost::archive::detail::load_pointer_type<Archive, Tptr>::non_abstract<T>::register_type(Archive&) [with T = ContainerDataT<char>, Archive = boost::archive::text_iarchive, Tptr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/iserializer.hpp:465: instantiated from ‘static const boost::archive::detail::basic_pointer_iserializer* boost::archive::detail::load_pointer_type<Archive, Tptr>::register_type(Archive&, T&) [with T = ContainerDataT<char>, Archive = boost::archive::text_iarchive, Tptr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/iserializer.hpp:485: instantiated from ‘static void boost::archive::detail::load_pointer_type<Archive, Tptr>::invoke(Archive&, Tptr&) [with Archive = boost::archive::text_iarchive, Tptr = ContainerDataT<char>*]’
/usr/include/boost/archive/detail/iserializer.hpp:572: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive, T = ContainerDataT<char>*]’
/usr/include/boost/archive/basic_text_iarchive.hpp:64: instantiated from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = ContainerDataT<char>*, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/text_iarchive.hpp:64: instantiated from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = ContainerDataT<char>*, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:76: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = ContainerDataT<char>*, Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:83: instantiated from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = ContainerDataT<char>*, Archive = boost::archive::text_iarchive]’
Container.h:52: instantiated from ‘void ContainerT<T>::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_iarchive, T = char]’
/usr/include/boost/serialization/access.hpp:109: instantiated from ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerT<char>]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = ContainerT<char>]’
/usr/include/boost/archive/detail/iserializer.hpp:160: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = ContainerT<char>]’
Main.cpp:110: instantiated from here
/usr/include/boost/archive/detail/iserializer.hpp:419: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
/usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = char [0u]]’:
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = char [0u]]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = char [0u]]’
/usr/include/boost/archive/detail/iserializer.hpp:160: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = char [0u]]’
Main.cpp:112: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘char [0u]’
/usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char [0u]]’:
/usr/include/boost/serialization/serialization.hpp:81: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char [0u]]’
/usr/include/boost/serialization/serialization.hpp:140: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = char [0u]]’
/usr/include/boost/archive/detail/oserializer.hpp:148: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = char [0u]]’
Main.cpp:112: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘char [0u]’

最佳答案

这里涉及的不仅仅是几个问题。

首先,

  • ContainerDataT<> 未能遵守三原则
  • ContainerT<> 未能遵守三原则
  • 在 C++ 中有 malloc/free。只需使用更安全的 new/delete(或者更好的是,以异常安全的方式管理所有权语义和生命周期的智能指针)。
  • T[] 作为成员等同于 T*

我建议使用标准类型来管理所有权语义 (unique_ptr),或者更好的是,只使用标准库来处理可变大小的动态分配数组(提示:它是 std::vector)。如果您坚持使用动态分配的数组成员,请诚实地告诉您的序列化函数:Live On Coliru

template<typename T>
class ContainerDataT : boost::noncopyable {
public:
ContainerDataT() : p_Data(new T[10]) { }
~ContainerDataT() { delete[] p_Data; }

T* GetData(){return p_Data;};

private:
friend class ContainerT<T>;

T* p_Data;

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version) {
ar & boost::serialization::make_array(p_Data, 10);
}
};

因此,您可以使用 boost::serialization::make_array 告诉 Boost Serialization 您的意图。

这是一个演示内容的简单演示程序(使用分配的固定大小的 T* p_Data )。请注意,我只是将 p_Data 设为 ContainerT<> 中的一个成员对象,因为它似乎没有双重动态实例化的好处。

#ifndef CONTAINER_H_
#define CONTAINER_H_

#include <boost/noncopyable.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/array.hpp>

template<typename T>
class ContainerT;

template<typename T>
class ContainerDataT : boost::noncopyable {
public:
ContainerDataT() : p_Data(new T[10]) {
std::copy_n("0123456789", 10, p_Data);
}
~ContainerDataT() { delete[] p_Data; }

T* GetData(){return p_Data;};

private:
friend class ContainerT<T>;

T* p_Data;

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version) {
ar & boost::serialization::make_array(p_Data, 10);
}
};

template<typename T>
class ContainerT {
public:
const T* GetMString() const { return p_Data.GetData(); }

private:
ContainerDataT<T> p_Data;

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version) {
ar & p_Data;
}
};

typedef ContainerT<char> MString;

#endif /* CONTAINER_H_ */

#ifndef PERSON_H_
#define PERSON_H_

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

//#include "Container.h"
class Person
{
public:
Person() = default;
~Person() = default;

private:
std::string z_Name;
int i_Age;
MString z_CountryCode;

//Serailization
//Allow serialization to access non-public data members.
friend class boost::serialization::access;

template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & z_Name;
ar & i_Age;
ar & z_CountryCode;
}
};

#endif /* PERSON_H_ */

int main()
{
boost::archive::text_oarchive oa(std::cout);

Person p;
oa << p;
}

关于c++ - 使用模板类 boost 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425278/

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