
Serialization of c style strings was excluded on purpose. The motivation for this was in large part the security issues involved. Then there was the problem of stings with spaces in them when writing to text output/input. You can so this with the binary_object wrappers which handles the length, memory pair. My view was that the std::string was as good or better than anything else I would use for this. Robert Ramey Tom Thorne wrote:
I'm trying to understand how to go about serializing allocated c style string buffers using the serialization library.
The example code below gives a compile error (see bottom) "left of '.serialize' must have class/struct/union type" and I don't see how to fix this. Could someone point me in the right direction?
In the meantime I'm using std::string, but this doesn't meet my needs.
Many thanks for any help. Tom
-----------------------------
#include <boost/serialization/string.hpp> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp>
template<class T> class test { public:
template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & boost::serialization::make_nvp("p", p); }
test() { p = 0; } ~test() { delete p; } explicit test(const T *t) { size_t len = std::char_traits<T>::length(t); p = new T[len+1]; std::char_traits<T>::copy(p, t, len+1); } const T *get() const { return p; } private:
T *p;
};
void sertest() { test<char> t("Hello");
std::ofstream ofs("test.txt"); boost::archive::text_oarchive oa(ofs);
oa << t; ofs.close();
}
------------------------------
------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling... sertest.cpp c:\Proj\boost\include\boost-1_32\boost\serialization\access.hpp(106) : error C2228: left of '.serialize' must have class/struct/union type type is 'type'
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(7 8) : see reference to function template instantiation 'void boost::serialization::access::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(1 21) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(15 2) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\serialization\extended_type_info_ typeid.hpp(100) : while compiling class-template member function '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=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(16 3) : see reference to class template instantiation 'boost::archive::detail::oserializer<Archive,T>' being compiled with [ Archive=boost::archive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(16 2) : while compiling class-template member function 'const boost::archive::detail::basic_oserializer &boost::archive::detail::pointer_oserializer<T,Archive>::get_basic_seria lizer(void) const' with [ T=type, Archive=boost::archive::text_oarchive ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(53 0) : see reference to class template instantiation 'boost::archive::detail::pointer_oserializer<T,Archive>' being compiled with [ T=type, Archive=boost::archive::text_oarchive ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(65) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer &boost::archive::detail::instantiate_pointer_oserializer<Archive,T>(Arch ive *,T *)' being compiled with [ Archive=boost::ar chive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(33 2) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer *boost::archive::detail::interface_oarchive<Archive>::register_type<T>(T *)' being compiled with [ Archive=boost::archive::text_oarchive, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(33 1) : while compiling class-template member function 'const boost::archive::detail::basic_pointer_oserializer *boost::archive::detail::save_pointer_type<Archive,TPtr>::non_abstract<T
register_type(Archive &)' with [ Archive=boost::archive::text_oarchive, TPtr=char *, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(34 9) : see reference to class template instantiation 'boost::archive::detail::save_pointer_type<Archive,TPtr>::non_abstract<T
' being compiled with [ Archive=boost::archive::text_oarchive, TPtr=char *, T=type ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(47 5) : see reference to function template instantiation 'const boost::archive::detail::basic_pointer_oserializer *boost::archive::detail::save_pointer_type<Archive,TPtr>::register_type<
(Archive &,T &)' being compiled with [ Archive=boost::archive::text_oarchive, TPtr=char *, T=char ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(46 2) : while compiling class-template member function 'void boost::archive::detail::save_pointer_type<Archive,TPtr>::invoke(Archive &,const TPtr)' with [ Archive=boost::archive::text_oarchive, TPtr=char * ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(55 1) : see reference to class template instantiation 'boost::archive::detail::save_pointer_type<Archive,TPtr>' being compiled with [ Archive=boost::archive::text_oarchive, TPtr=char * ]
c:\Proj\boost\include\boost-1_32\boost\archive\basic_text_oarchive.hpp(7 1) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,const T & )' being compiled with [ Archive=boost::archive::text_oarchive, T=char * ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(80) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(const T & ,int)' being compiled with [ Archive=boost::archive::text_oarchive, T=char * ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(110) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<T>(const T & )' being compiled with [ Archive=boost::archive::text_oarchive, T=char * ]
c:\Proj\boost\include\boost-1_32\boost\serialization\nvp.hpp(58)
see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator &<T>(T & )' being compiled with [ Archive=boost::archive::text_oarchive, T=char * ]
c:\Proj\boost\include\boost-1_32\boost\serialization\access.hpp(106) : see reference to function template instantiation 'void boost::serialization::nvp<T>::serialize<Archive>(Archive &,const unsigned int) const' being compiled with [ T=char *, Archive=boost::archive::text_oarchive ]
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(7 8) : see reference to function template instantiation 'void boost::serialization::access::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(1 21) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(22 7) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(22 2) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::save_only::inv okex(Archive &,const T &)' with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(30 9) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>::save_only' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(23 7) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::invoke(Archive &,const T &)' with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(55 1) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\basic_text_oarchive.hpp(7 1) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,const T &)' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(87) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(const T &,int)' being compiled with [ Archive=boost::archive::text_oarchive, T=boost::serialization::nvp<char *> ] c:\Proj\lnp\core\test\sertest.cpp(14) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator &<boost::serialization::nvp<T>>(const boost::serialization::nvp<T> &)' being compiled with [ Archive=boost::archive::text_oarchive, T=char * ]
c:\Proj\boost\include\boost-1_32\boost\serialization\access.hpp(106) : see reference to function template instantiation 'void test<T>::serialize<Archive>(Archive &,const unsigned int)' being compiled with [ T=char, Archive=boost::archive::text_oarchive ]
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(7 8) : see reference to function template instantiation 'void boost::serialization::access::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\serialization\serialization.hpp(1 21) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(15 2) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\serialization\extended_type_info_ typeid.hpp(100) : while compiling class-template member function '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=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(23 4) : see reference to class template instantiation 'boost::archive::detail::oserializer<Archive,T>' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(23 3) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::save::invokex( Archive &,const T &)' with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(30 9) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>::save' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(23 7) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::invoke(Archive &,const T &)' with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\oserializer.hpp(55 1) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\basic_text_oarchive.hpp(7 1) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,const T &)' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(80) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(const T &,int)' being compiled with [ Archive=boost::archive::text_oarchive, T=test<char> ]
c:\Proj\boost\include\boost-1_32\boost\archive\detail\interface_oarchive .hpp(105) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<test<T>>(const test<T> &)' being compiled with [ Archive=boost::archive::text_oarchive, T=char ] c:\Proj\lnp\core\test\sertest.cpp(55) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<test<T>>(test<T> &)' being compiled with [ Archive=boost::archive::text_oarchive, T=char ]
test - 1 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped