我认为boost :: iostreams的pipelines很方便,但似乎无法将std::cin
或std::cout
用作Device
,例如在我的代码中:
#include <iostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/counter.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
namespace io = boost::iostreams;
int main()
{
// NG: using pipelines and std::cin
io::filtering_istream in(io::counter()|std::cin);
// OK: not using pipelines
//io::filtering_istream in;
//in.push(io::counter());
//in.push(std::cin);
// OK: not using std::cin
//io::filtering_istream in(io::counter()|io::file_source("/etc/hosts"));
io::copy(in, std::cout);
std::cout << in.component<0, io::counter>()->lines() << '\n';
std::cout << in.component<0, io::counter>()->characters() << '\n';
}
只是好奇:我真的不能将std::cin
与管道结合使用吗?
clang ++ 8抱怨:
In file included from prog.cc:3:
In file included from /opt/wandbox/boost-1.73.0/clang-8.0.0/include/boost/iostreams/filter/counter.hpp:19:
/opt/wandbox/boost-1.73.0/clang-8.0.0/include/boost/iostreams/pipeline.hpp:95:13: error: no matching member function for call to 'push'
chn.push(component_);
~~~~^~~~
:
/opt/wandbox/boost-1.73.0/clang-8.0.0/include/boost/iostreams/chain.hpp:484:33: note: candidate template ignored: deduced type 'basic_istream<...>' of 1st parameter does not match adjusted type 'const basic_istream<...>' of argument [with CharType = char, TraitsType = std::__1::char_traits<char>]
我不知道为什么,但是只有boost::ref(std::cin)
有效!