Using boost::iostreams::tee_device?

后端 未结 2 1428
無奈伤痛
無奈伤痛 2020-12-05 21:12

Can someone help me?

I am trying to do something like the following:

#include 
#include 

        
相关标签:
2条回答
  • 2020-12-05 22:12

    You use the constructor-forwarding version of io::stream, which construct a tee-stream itself and forward all arguments to that. C++03 has only limited capabilities when it comes to forwarding arguments to functions (amount of overloads needed easily grow exponentially). It (io::stream) makes the following restrictions:

    Each of these members constructs an instance of stream and associates it with an instance of the Device T constructed from the given lists of arguments. The T constructors involved must take all arguments by value or const reference.

    Well, but the tee_device constructor says

    Constructs an instance of tee_device based on the given pair of Sinks. Each function parameter is a non-const reference if the corresponding template argument is a stream or stream buffer type, and a const reference otherwise.

    That won't work, of course. io::stream provides another constructor that takes a T as first argument. This works here (Compiles, at least. The assertion fails, though. I've not worked with boost::iostreams so i can't help with that)

    namespace io = boost::iostreams;
    typedef io::tee_device<std::stringstream, std::stringstream> TeeDevice;
    typedef io::stream< TeeDevice > TeeStream;
    std::stringstream ss1, ss2;
    TeeDevice my_tee(ss1, ss2); 
    TeeStream my_split(my_tee);
    my_split << "Testing";
    assert(ss1.str() == "Testing" && ss1.str() == ss2.str());
    

    Edit: After calling flush() or streaming << std::flush, the assertion passes.

    0 讨论(0)
  • 2020-12-05 22:13

    Probably you need to set it up like this:

    typedef io::tee_device<std::stringstream, std::stringstream> Tee;
    typedef io::stream<Tee> TeeStream;
    
    std::stringstream ss1, ss2;
    Tee my_tee(ss1, ss2);
    TeeStream my_split(my_tee);
    
    0 讨论(0)
提交回复
热议问题