我希望扩展Boost序列化库,使STL集合以不同于Boost序列化库提供的格式保存到XML档案。
如果我是正确的,所有STL容器在序列化期间都会传递以下函数:
// <boost/serialization/collections_save_imp.hpp>
namespace boost{ namespace serialization { namespace stl {
template<class Archive, class Container>
inline void save_collection(Archive & ar, const Container &s)
{
/* ... */
}
} } }
所以我试图为xml_oarchive
重载此函数。这是我的方法的一个小例子:
#include <iostream>
#include <vector>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/vector.hpp>
namespace boost { namespace serialization { namespace stl {
template< typename Container >
inline void save_collection( boost::archive::xml_oarchive& ar, Container const& s )
{
/* My serialization */
}
} } }
int main()
{
{
boost::archive::xml_oarchive ar( std::cout );
std::vector< int > x;
x.push_back( -1 );
x.push_back( 1 );
x.push_back( 42 );
x.push_back( 0 );
ar << BOOST_SERIALIZATION_NVP( x );
}
return 0;
}
它编译并运行。但它并没有调用我的功能,而是Boost提供的功能。我需要做什么/更改以使我的STL容器序列化工作?
答案 0 :(得分:0)
最后,我想出了解决问题的方法:
#include <iostream>
#include <vector>
namespace boost { namespace archive { class xml_oarchive; } }
namespace boost { namespace serialization { namespace stl {
/* Two template parameters are needed here because at the caller side
* a function with two template parameters is explicitly requested. */
template< typename, typename Container >
void save_collection( boost::archive::xml_oarchive&, Container const& )
{
/* ... */
}
} } }
/* Note that this is before the boost includes. */
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/vector.hpp>
int main()
{
{
boost::archive::xml_oarchive ar( std::cout );
std::vector< int > x;
x.push_back( -1 );
x.push_back( 1 );
x.push_back( 42 );
x.push_back( 0 );
ar << BOOST_SERIALIZATION_NVP( x );
}
return 0;
}