- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为 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<>
未能遵守三原则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/
我正在尝试使用boost.spirit的qi库解析某些内容,而我遇到了一个问题。根据spirit docs,a >> b应该产生类型为tuple的东西。但这是boost::tuple(又名 fusio
似乎有/正在努力做到这一点,但到目前为止我看到的大多数资源要么已经过时(带有死链接),要么几乎没有信息来实际构建一个小的工作样本(例如,依赖于boost program_options 以构建可执行文
我对 Boost.Log 的状态有点困惑。这是 Boost 的官方部分,还是尚未被接受?当我用谷歌搜索时,我看到一些帖子谈论它在 2010 年是如何被接受的,等等,但是当我查看最后一个 Boost 库
Boost 提供了两种不同的实现 string_view ,这将成为 C++17 的一部分: boost::string_ref在 utility/string_ref.hpp boost::stri
最近,我被一家GIS公司雇用来重写他们的旧地理信息库。所以我目前正在寻找一个好的计算几何库。我看过CGAL,这真是了不起,但是我的老板想要免费的东西。 所以我现在正在检查Boost.Geometry。
假设我有一个无向图 G。假设我添加以下内容 add_edge(1,2,G); add_edge(1,3,G); add_edge(0,2,G); 现在我再说一遍: add_edge(0,2,G); 我
我使用 CMake 来查找 Boost。找到了 Boost,但 CMake 出错了 Imported targets not available for Boost version 请参阅下面的完整错
我是 boost::fusion 和 boost::mpl 库的新手。谁能告诉我这两个库之间的主要区别? 到目前为止,我只使用 fusion::vector 和其他一些简单的东西。现在我想使用 fus
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What are the benefits of using Boost.Phoenix? 所以我开始阅读 boos
我正在尝试获得一个使用 Boost.Timer 的简单示例,用于一些秒表性能测量,但我不明白为什么我无法成功地将 Boost.Timer 链接到 Boost.Chrono。我使用以下简单脚本从源代码构
我有这样的东西: enum EFood{ eMeat, eFruit }; class Food{ }; class Meat: public Food{ void someM
有人可以告诉我,我如何获得boost::Variant处理无序地图? typedef boost::variant lut_value;unordered_map table; 我认为有一个用于boo
我对 Boost.Geometry 中的环和多边形感到困惑。 在文档中,没有图形显示什么是环,什么是多边形。 谁能画图解释两个概念的区别? 最佳答案 在 Boost.Geometry 中,多边形被定义
我正在使用 boost.pool,但我不知道何时使用 boost::pool<>::malloc和 boost::pool<>::ordered_malloc ? 所以, boost::pool<>:
我正在尝试通过 *boost::fast_pool_allocator* 使用 *boost::container::flat_set*。但是,我收到编译错误。非常感谢您的意见和建议。为了突出这个问题
sau_timer::sau_timer(int secs, timerparam f) : strnd(io), t(io, boost::posix_time::seconds(secs)
我无法理解此功能的文档,我已多次看到以下内容 tie (ei,ei_end) = out_edges(*(vi+a),g); **g**::out_edge_iterator ei, ei_end;
我想在 C++ 中序列化分层数据结构。我正在处理的项目使用 boost,所以我使用 boost::property_tree::ptree 作为我的数据节点结构。 我们有像 Person 这样的高级结
我需要一些帮助来解决这个异常,我正在实现一个 NPAPI 插件,以便能够使用来自浏览器扩展的本地套接字,为此我正在使用 Firebreath 框架。 对于套接字和连接,我使用带有异步调用的 Boost
我尝试将 boost::bind 与 boost::factory 结合使用但没有成功 我有这个类 Zambas 有 4 个参数(2 个字符串和 2 个整数)和 class Zambas { publ
我是一名优秀的程序员,十分优秀!