c++ - boost::iostreams::filtering_stream 的底层流的类型是什么?

标签 c++ boost c++11 boost-iostreams

我有一个使用 Boost.Iostreams 执行解压缩的流:

struct istream_zlib
  : public boost::iostreams::filtering_stream<boost::iostreams::input, char>
{
  istream_zlib(std::istream& in)
  {
    push(boost::iostreams::zlib_decompressor());
    push(in);
  }
};

现在,我想稍后访问底层流(std::istream& in)。天真地,我认为通过 component() 请求 std::istream 就可以了,但我得到的指针是 null:

auto ptr = component<std::istream>(1); // ptr is null!

我应该向 component() 提供什么类型来执行此操作?

最佳答案

这不是真的,因为不是istream将被插入filtering_stream (例如,对于我的 boost 1.48,它将是 boost::iostreams::detail::mode_adapter<boost::iostreams::input, std::istream>),您可以通过 component_type 检查它的类型。功能。但是,我不知道为什么你需要获取 stream来自filtering_stream ,因为您发送了引用 - 您应该在使用此 filtering_stream 的地方拥有此对象.

此外,您还可以使用reference_wrapper对于这种情况(即 push(boost::ref(in)); ),然后使用组件获取它

auto ptr = component<boost::reference_wrapper<std::istream>>(1);

关于c++ - boost::iostreams::filtering_stream 的底层流的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587560/

相关文章:

c++ - Lua 和伪造的 Typecast

c++ - 为什么这有效?

c++ - 在 "if"语句中调用 std::set 函数时的意外评估结果

c++ - 错误 : no matching function for call to [. ..] 注意:模板参数推导/替换失败

c++ - 修复了 VSync 打开时的时间步长卡顿

c++ - 无法在 C++ 中中断 while 循环

c++ - 无法将十六进制数据转移到无符号长整型中

C++:多线程和引用计数

c++ - Boost.Compute 比普通 CPU 慢?

c++ - 在 Visual Studio 中执行单个 Boost Test 单元测试