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