我正在玩 boost::iostreams 和用户指南谈论过滤器“计数器”。所以我用这段代码试试:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out(cnt | io::null_sink());
out << "hello";
std::cout << cnt.lines() << " " << cnt.characters() << std::endl;
}
它总是给
0 0
这似乎不是我所期待的。使用 gdb 进行的初步跟踪表明正在进行计数的计数器对象具有与对象“cnt”不同的地址。我想这是管道中的某种复制?如果是这样,过滤“计数器”有什么用?