How to pipe into std::cout with boost::iostreams

前端 未结 3 1752
小蘑菇
小蘑菇 2021-01-14 05:16

I am new to boost::iostreams so this might be trivial:

Assuming namespace io = boost::iostreams;

this works

io::fil         


        
相关标签:
3条回答
  • 2021-01-14 05:48

    Wrapping std::cout with boost::ref worked for me:

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

    See note_1 in pipable docs for details.

    0 讨论(0)
  • 2021-01-14 05:51

    That's not the way you pass the stream. You have to use push:

    out.push(std::cout);
    
    0 讨论(0)
  • 2021-01-14 05:56

    For the sake of completeness, a simple "Sink wrapper" looks like this:

    #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); }
    

    And can be used like this:

    boost::iostreams::filtering_ostream  out(filter | wrap_sink(std::cout));
    
    0 讨论(0)
提交回复
热议问题