c++ - 如何使用 boost::iostreams 管道进入 std::cout

标签 c++ boost iostream boost-iostreams

我是 boost::iostreams 的新手,所以这可能是微不足道的:

假设 namespace io = boost::iostreams;

这行得通

io::filtering_ostream out(std::cout);
out << "some\nstring\n";

这行得通

std::string result;
io::filtering_ostream out(io::counter() | io::back_inserter(result));
out << "some\nstring\n";

然而这并不能编译

io::filtering_ostream out(io::counter() | std::cout);
out << "some\nstring\n";

如何通过管道传输到 std::cout

最佳答案

boost::ref 包装 std::cout为我工作:

io::filtering_ostream out(DummyOutputFilter() | boost::ref(std::cout));

参见 pipable docs 中的注释 1了解详情。

关于c++ - 如何使用 boost::iostreams 管道进入 std::cout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21031502/

相关文章:

c++ - QObject::connect:没有这样的信号运行时警告 - 找不到原因

c++ - 无需继承直接访问类接口(interface)中成员的成员

c++ - 为什么我不能将数组传递给函数模板?

C++ 类文件

boost - 2> LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-sgd-1_58.lib'

c++ - boost : persistent storage of R-trees?

c++ - 使用 boost::lambda 复制容器

c++ - 在 C++ 中循环遍历 .txt 文件的行

c++ - 与不同架构上的数据对齐相关的问题是什么?

c++ - std::可选的流插入运算符 >>