在处理二进制数据时,我倾向于使用uint8_t
. 我想这对我来说似乎是一个字节的类型。但是,我无法弄清楚如何让二进制序列化在 boost 中使用该类型。我是在问不可能的事情还是只是错过了一些简单的事情?
此示例程序无法在 VS2013 上编译,并抱怨不会将流转换为std::ostream
or std::istream
。
该程序可以正常工作,char
而不是uint8_t
,但这让我很烦恼。:)
#include <cstdlib> // EXIT_SUCCESS
#include <vector>
#include "boost/archive/binary_iarchive.hpp"
#include "boost/archive/binary_oarchive.hpp"
#include "boost/iostreams/device/array.hpp"
#include "boost/iostreams/device/back_inserter.hpp"
#include "boost/iostreams/stream_buffer.hpp"
int main(int argc, char** argv)
{
typedef std::vector< uint8_t > Buffer;
Buffer buffer;
// Serialization
{
int foo = 1;
typedef boost::iostreams::back_insert_device< Buffer > Device;
Device device(buffer);
typedef boost::iostreams::stream_buffer< Device > Stream;
Stream stream(device);
{
boost::archive::binary_oarchive archive(stream);
archive << foo;
}
}
// Deserialization
{
int foo;
typedef boost::iostreams::basic_array_source< uint8_t > Device;
Device device(buffer.data(), buffer.size());
typedef boost::iostreams::stream_buffer< Device > Stream;
Stream stream(device);
{
boost::archive::binary_iarchive archive(stream);
archive >> foo;
}
}
return EXIT_SUCCESS;
}