- 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/
namespace std { template <> class hash{ public : size_t operator()( cons
我正在构建一个 Javascript 交互性有限的 Django 应用程序,并且正在研究如何将 Vue 模板与 Django 模板合并以实现相同的内容。 想象一个无限滚动的页面,其中 SEO 非常重要
我需要一个由游戏逻辑组成的外部类,调用 LitElement 组件,并向其传递一个 html 模板文字,该组件将使用该文字来更新其自己的 html 模板文字的一部分。 在下面的代码中,您将看到组件的一
很简单,我不想在 html 文件中定义所有 Handlebars 模板 我试过了 但这并没有奏效。我是否可以不以编程方式定义模板,甚至只是加载 Handlebars 文件,以便我可以重用,而且我觉得
在此代码中,j 正确地成为对象:j.name、j.addr、j.city、j.state 和 j.zip。但是,成功函数有一个 JavaScript 错误 .tmpl() 不是函数。 {{t
Django模板不会?点进来,总结了模板语法传值取值、过滤器和自定义过滤器、模板标签的分类、中间件403报错如何解决、如何继承模板~👆 Django 模板 模板传值取值 后端传值 键值对形式:{‘n
哈喽大家好,我是鹿 九 丸 \color{red}{鹿九丸}鹿九丸,今天给大家带来的是C++模板。 如果大家在看我的博客的过程中或者学习的过程中以及在学习方向上有什么问题或者想跟我交流的话可以加我的企
我正在用 PHP 编写一个简单的模板层,但我遇到了一些困难。目前它是这样工作的: 首先,我使用 fetch_template 从数据库中加载模板内容 - 这可行(如果您有兴趣,我会在启动时收集所有模板
我正在制作有关模板的 Django 教程。我目前处于此代码: from django.template import Template, Context >>> person = {'name': '
我正在使用 Jquery 模板来显示传入的 JSON 数据我想将模板加载到可缓存的外部文件中。我该怎么做? 更新 http://encosia.com/2010/12/02/jquery-templa
这是我的观点.py: from django.http import HttpResponse from django.template.loader import get_template from
我试图说服一位同事在项目的前端使用 Mustache/Hogan,我提出了以下建议: 有一个 templates.js 文件,大致如下所示: var tpl_alert = '{{msg}}'; va
我想创建一个通用的数组函数。在我的 API 中,我有一个通用容器,我需要将其转换为正确的类,但我想让它通用 template void UT::printArray(CCArray* arr, T t
有谁知道是否有办法在 Genshi 中创建 javascript 模板?我的意思是,我需要一个 .js 文件,可以在其中使用 等指令。等等。 有什么想法吗?谢谢! 最佳答案 你可以直接在html中这
我想知道是否可以设置某种 HTML 模板系统,基本上我有 3 个不同的文件: - header.html - footer.html - landing.html(landing.html 是包含页面
我正在尝试构建以下 HTML 模板: 这很简单,如果我使用红色容器 1-4,语法如下: 1 2 3 4 5 6 7 8 9 https://jsfi
#include "boost/numeric/ublas/matrix.hpp" using namespace boost::numeric::ublas; template class Lay
我在一个类中有一个函数,它传递了一个函数及其参数,然后将它们绑定(bind)到一个函数调用中并调用该函数等。 这已经被快速组合在一起以测试我知道代码不是很好的概念。 class Profiling {
是否有一个 c++ 结构或模板(在任何库中)允许我在十进制和任何其他基数之间进行转换(很像 bitset 可以做的)? 最佳答案 是的,你可以使用unsigned int: unsigned int
数据类型给程序设计带来的困扰及解决方案 int maxt(int, int); double maxt(double, double); 若有一种占位符T,能够代替类型,便可以简化代码的冗余编写
我是一名优秀的程序员,十分优秀!