首页 > 解决方案 > Boost Iostreams component() 调用失败

问题描述

我正在尝试访问std::ifstream添加到boost::iostreams::filtering_streambuf链中的底层。根据常见问题解答,一种方法是使用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

在 Coliru 上运行

标签: c++boostiostream

解决方案


推荐阅读