3

我是新手,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

4

3 回答 3

7

std::coutboost::ref包装对我有用:

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

有关详细信息,请参阅pipable 文档中的 note_1 。

于 2014-10-04T23:55:23.413 回答
2

为了完整起见,一个简单的“Sink wrapper”如下所示:

#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/pipeline.hpp>

template<typename Sink>
class sink_wrapper
    : public boost::iostreams::device<boost::iostreams::output, typename Sink::char_type> {
public:
    sink_wrapper(Sink & sink) : sink_(sink) {}

    std::streamsize write(const char_type * s, std::streamsize n) {
        sink_.write(s, n);
        return n;
    }

private:
    sink_wrapper & operator=(const sink_wrapper &);
    Sink & sink_;
};
BOOST_IOSTREAMS_PIPABLE(sink_wrapper, 1)

template<typename S> sink_wrapper<S> wrap_sink(S & s) { return sink_wrapper<S>(s); }

并且可以这样使用:

boost::iostreams::filtering_ostream  out(filter | wrap_sink(std::cout));
于 2014-01-13T17:00:46.920 回答
1

这不是你传递流的方式。你必须使用push

out.push(std::cout);
于 2014-01-09T21:35:08.640 回答