3

在处理二进制数据时,我倾向于使用uint8_t. 我想这对我来说似乎是一个字节的类型。但是,我无法弄清楚如何让二进制序列化在 boost 中使用该类型。我是在问不可能的事情还是只是错过了一些简单的事情?

此示例程序无法在 VS2013 上编译,并抱怨不会将流转换为std::ostreamor 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;
}
4

1 回答 1

4

二进制档案带有std::istream/std::ostream参数,因此您无法使用std::basic_istream<char>/ std::basic_ostream<char>

顺便说一句,不能保证实现提供流的unsigned char/uint8_t版本(或者更确切地说std::char_traits<unsigned char>)。

于 2014-07-25T05:32:01.133 回答