У меня есть следующий тестовый код (полностью компилируемый):
#include "stdafx.h"
#include <memory>
#include <vector>
#include <string>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/map.hpp>
typedef unsigned int addressvalue_t;
using namespace std;
class base {
public:
virtual int get()=0;
};
class sub1:public base {
int a,b;
std::shared_ptr<vector<addressvalue_t>> addressListPtr;
std::shared_ptr<vector<unsigned char>> commonData;
public:
sub1(int a,int b):a(a),b(b){}
virtual int get() {
return a*b*10;
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & a;
ar & b;
ar & addressListPtr;
ar & commonData;
}
void serializeMe() {
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
oa << *this;
}
};
int _tmain(int argc, _TCHAR* argv[]){
sub1 * b;
b=new sub1(22,33);
#if 0
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
oa << *b;
#else
b->serializeMe();
#endif
return 0;
}
Когда я скомпилировал его, используя VC ++ 2010 и boost 1.68.Компиляция завершилась успешно, но загадочно выдало следующее предупреждение:
1>------ Build started: Project: cpptest, Configuration: Debug Win32 ------
1> cpptest.cpp
1>c:\bin\boost_1_68_0\boost\mpl\print.hpp(55): warning C4308: negative integral constant converted to unsigned type
1> c:\bin\boost_1_68_0\boost\serialization\static_warning.hpp(88) : see reference to class template instantiation 'boost::mpl::print<T>' being compiled
1> with
1> [
1> T=boost::serialization::BOOST_SERIALIZATION_STATIC_WARNING_LINE<137>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\check.hpp(137) : see reference to class template instantiation 'boost::serialization::static_warning_test<B,L>' being compiled
1> with
1> [
1> B=false,
1> L=137
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(460) : see reference to function template instantiation 'void boost::archive::detail::check_pointer_level<T>(void)' being compiled
1> with
1> [
1> T=std::vector<unsigned int>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(480) : see reference to function template instantiation 'void boost::archive::detail::save_pointer_type<Archive>::save<T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::vector<unsigned int>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(539) : see reference to function template instantiation 'void boost::archive::detail::save_pointer_type<Archive>::invoke<const T>(Archive &,const TPtr)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *,
1> TPtr=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\common_oarchive.hpp(71) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\archive\basic_text_oarchive.hpp(83) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\interface_oarchive.hpp(70) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<const T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\nvp.hpp(67) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<T>(const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\access.hpp(91) : see reference to function template instantiation 'void boost::serialization::nvp<T>::save<Archive>(Archive &,const unsigned int) const' being compiled
1> with
1> [
1> T=const std::vector<unsigned int> *,
1> Archive=boost::archive::text_oarchive
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_member.hpp(43) : see reference to function template instantiation 'void boost::serialization::access::member_save<Archive,const T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_member.hpp(42) : while compiling class template member function 'void boost::serialization::detail::member_saver<Archive,T>::invoke(Archive &,const T &,const unsigned int)'
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_member.hpp(69) : see reference to class template instantiation 'boost::serialization::detail::member_saver<Archive,T>' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\nvp.hpp(76) : see reference to function template instantiation 'void boost::serialization::split_member<Archive,boost::serialization::nvp<T>>(Archive &,boost::serialization::nvp<T> &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\access.hpp(116) : see reference to function template instantiation 'void boost::serialization::nvp<T>::serialize<Archive>(Archive &,const unsigned int)' being compiled
1> with
1> [
1> T=const std::vector<unsigned int> *,
1> Archive=boost::archive::text_oarchive
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\serialization.hpp(68) : see reference to function template instantiation 'void boost::serialization::access::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\serialization.hpp(126) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(250) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(315) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::save_only::invoke<T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(539) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::invoke<const T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\common_oarchive.hpp(71) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\basic_text_oarchive.hpp(83) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\interface_oarchive.hpp(70) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<const T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=boost::serialization::nvp<const std::vector<unsigned int> *>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\shared_ptr.hpp(236) : 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
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=const std::vector<unsigned int> *
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_free.hpp(45) : see reference to function template instantiation 'void boost::serialization::save<Archive,std::vector<_Ty>>(Archive &,const std::tr1::shared_ptr<std::vector<_Ty>> &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> _Ty=unsigned int
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_free.hpp(41) : while compiling class template member function 'void boost::serialization::free_saver<Archive,T>::invoke(Archive &,const T &,const unsigned int)'
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\split_free.hpp(74) : see reference to class template instantiation 'boost::serialization::free_saver<Archive,T>' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\shared_ptr.hpp(273) : see reference to function template instantiation 'void boost::serialization::split_free<Archive,std::tr1::shared_ptr<_Ty>>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> _Ty=std::vector<unsigned int>,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\serialization.hpp(126) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,std::vector<_Ty>>(Archive &,std::tr1::shared_ptr<std::vector<_Ty>> &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> _Ty=unsigned int
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(157) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(149) : while compiling class template member function 'void boost::archive::detail::oserializer<Archive,T>::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const'
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(263) : see reference to class template instantiation 'boost::archive::detail::oserializer<Archive,T>' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(315) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::save_standard::invoke<T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(539) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::invoke<const T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\common_oarchive.hpp(71) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\basic_text_oarchive.hpp(83) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\interface_oarchive.hpp(70) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<const T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\interface_oarchive.hpp(77) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<T>(const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\work\cpptest\cpptest\cpptest.cpp(43) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator &<std::tr1::shared_ptr<_Ty>>(const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> _Ty=std::vector<unsigned int>,
1> T=std::tr1::shared_ptr<std::vector<unsigned int>>
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\access.hpp(116) : see reference to function template instantiation 'void sub1::serialize<Archive>(Archive &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\serialization.hpp(68) : see reference to function template instantiation 'void boost::serialization::access::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\serialization\serialization.hpp(126) : see reference to function template instantiation 'void boost::serialization::serialize<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(157) : see reference to function template instantiation 'void boost::serialization::serialize_adl<Archive,T>(Archive &,T &,const unsigned int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(149) : while compiling class template member function 'void boost::archive::detail::oserializer<Archive,T>::save_object_data(boost::archive::detail::basic_oarchive &,const void *) const'
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(263) : see reference to class template instantiation 'boost::archive::detail::oserializer<Archive,T>' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(315) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::save_standard::invoke<T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\oserializer.hpp(539) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::invoke<const T>(Archive &,const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\common_oarchive.hpp(71) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\basic_text_oarchive.hpp(83) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\bin\boost_1_68_0\boost\archive\detail\interface_oarchive.hpp(70) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<const T>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> c:\work\cpptest\cpptest\cpptest.cpp(49) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<sub1>(const T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=sub1
1> ]
1> Creating library c:\work\cpptest\Debug\cpptest.lib and object c:\work\cpptest\Debug\cpptest.exp
1> cpptest.vcxproj -> c:\work\cpptest\Debug\cpptest.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
После некоторых проб и ошибок я обнаружил, что если я закомментирую следующие 2 члена в моей функции serialize
, предупреждение исчезнет:
//ar & addressListPtr;
//ar & commonData;
Почему появилось такое предупреждение и что случилось с моим кодом?
Кроме того, мой базовый класс состоит только из объявлений чистой виртуальной функции и не содержит переменных-членов, нет необходимости его сериализациив любом случае.Вся сериализация выполняется в 1 из производных классов.Поэтому я решил использовать функцию-член serializeMe()
, это плохой стиль?
Редактировать: Дополнительная информация:
line 57 of ...\boost\mpl\print.hpp
template <class T>
struct print
: mpl::identity<T>
#if defined(__MWERKS__)
, aux::print_base
#endif
{
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wc++11-extensions"
const int m_x = 1 / (sizeof(T) - sizeof(T));
# pragma clang diagnostic pop
#elif defined(BOOST_MSVC)
enum { n = sizeof(T) + -1 };//<---------line 57
#elif defined(__MWERKS__)
void f(int);
#else
enum {
n =
# if defined(__EDG_VERSION__)
aux::dependent_unsigned<T>::value > -1
# else
sizeof(T) > -1
# endif
};
#endif
};
строка 137 из check.hpp
// Address the following when serializing to a pointer:
// a) This type doesn't save class information in the
// archive. That is, the serialization trait implementation
// level <= object_serializable.
// b) Tracking for this type is set to "track selectively"
// in this case, indication that an object is tracked is
// not stored in the archive itself - see level == object_serializable
// but rather the existence of the operation ar >> T * is used to
// infer that an object of this type should be tracked. So, if
// you save via a pointer but don't load via a pointer the operation
// will fail on load without given any valid reason for the failure.
// So if your program traps here, consider changing the
// tracking or implementation level traits - or not
// serializing via a pointer.
BOOST_STATIC_WARNING(typex::value); //<---------line137
}