【问题标题】:writing a gz stream with boost用 boost 写一个 gz 流
【发布时间】:2013-03-13 19:25:00
【问题描述】:

我编写了一个代码,使用boost 将输出写入gz 文件。

class foo() {
  std::ofstream traceOut;
  struct traceRec {
    traceRec(uint64_t c) : cycle(c) {};
    uint64_t cycle;
  };
  void writeTrace(traceRec &rec)
  {
    try {
      boost::iostreams::filtering_istream in;
      in.push(boost::iostreams::gzip_decompressor());
      in.push(traceOut);   // ERROR
      std::cout << rec.cycle << std::endl;
    }
    catch(const boost::iostreams::gzip_error& e) {
      std::cout << e.what() << '\n';
    }
  }
  void init() {
    traceOut.open("log.gz", std::ios_base::in | std::ios_base::binary);
  }
  void bar() {
    traceRec rec (1000);
    writeTrace(rec);
  }
};

但是编译时出现这个错误

/opt/boost_1_33_1/boost/iostreams/chain.hpp: In member function ‘void boost::iostreams::detail::chain_client<Chain>::push(std::basic_ostream<_CharT2,      _Traits2>&, int, int) [with CharType = char, TraitsType = std::char_traits<char>, Chain = boost::iostreams::chain<boost::iostreams::input, char, 
std::char_traits<char>, std::allocator<char> >]’:
test.cpp:   instantiated from here
/opt/boost_1_33_1/boost/iostreams/chain.hpp:472: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’ 
/opt/boost_1_33_1/boost/iostreams/traits.hpp: At global scope:
/opt/boost_1_33_1/boost/iostreams/traits.hpp: In instantiation of ‘boost::iostreams::detail::member_category<mpl_::void_>’:
/opt/boost_1_33_1/boost/mpl/eval_if.hpp:38:   instantiated from ‘boost::mpl::eval_if<boost::iostreams::is_std_io<mpl_::void_>, boost::iostreams::select<boost::iostreams::is_iostream<mpl_::void_>, boost::iostreams::iostream_tag, boost::iostreams::is_istream<mpl_::void_>, boost::iostreams::istream_tag, boost::iostreams::is_ostream<mpl_::void_>, boost::iostreams::ostream_tag, boost::iostreams::is_streambuf<mpl_::void_>, boost::iostreams::streambuf_tag, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_, mpl_::bool_<true>, mpl_::void_>, boost::iostreams::detail::member_category<mpl_::void_> >’
....

这是什么意思?

【问题讨论】:

    标签: c++ boost gzip


    【解决方案1】:

    请看this,push() 方法必须接收流而不是任意对象

    EDIT1(在 cmets 交换之后)

    traceOut 的类型更改为std::ifstream 可以解决构建问题(至少使用boost 1.52)

    【讨论】:

    • std::ofstream 不符合流的条件吗?
    • @mahmood 不,我不是那个意思 :) 根据 boost static 检查,似乎 boost 期望他们自己的 iostream 对象和 std ofstream 不是“可接受的”模板,所以我投票使用 boost iostream对象)
    • 输入和输出流的组合也可能导致这个问题(我希望 boost doc 更详细)
    • @mahmood 是个好公民 :) 你能接受我的帖子吗?因为它清楚地说明了你的发现
    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多