代码之家  ›  专栏  ›  技术社区  ›  tangy

Boost Iostreams component()调用失败

  •  0
  • tangy  · 技术社区  · 7 年前

    std::ifstream 添加到 boost::iostreams::filtering_streambuf 链条根据 FAQ ,一种方法是使用 component/component_type . 为什么下面的代码会失败?

    std::ifstream fp{"input.txt", std::ios_base::in | std::ios_base::binary};
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
    in.push(boost::iostreams::counter{});
    in.push(fp);
    auto *c = in.component<boost::iostreams::counter>(0); // works
    std::cout << (c == nullptr) << std::endl; // 0
    std::cout << c->characters() << std::endl;
    std::cout << c->lines() << std::endl;
    
    // Look into ifstream
    auto *f_ptr = in.component<std::ifstream>(1); // fails
    std::cout << (f_ptr == nullptr) << std::endl; // 1
    

    Run on Coliru

    0 回复  |  直到 7 年前