简体   繁体   English

使用 boost::iostreams::tee_device?

[英]Using boost::iostreams::tee_device?

Can someone help me?有人能帮我吗?

I am trying to do something like the following:我正在尝试执行以下操作:

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>  
#include <cassert>  

namespace io = boost::iostreams;
typedef io::stream<io::tee_device<std::stringstream, std::stringstream> > Tee;
std::stringstream ss1, ss2;
Tee my_split(ss1, ss2); // redirects to both streams
my_split << "Testing";
assert(ss1.str() == "Testing" && ss1.str() == ss2.str());

But it won't compile in VC9:但它不会在 VC9 中编译:

c:\lib\boost_current_version\boost\iostreams\stream.hpp(131) : error C2665: 'boost::iostreams::tee_device<Sink1,Sink2>::tee_device' : none of the 2 overloads could convert all the argument types

Has anyone gotten this to work?有没有人让这个工作? I know I could make my own class to do it, but I want to know what I am doing wrong.我知道我可以制作自己的 class 来做到这一点,但我想知道我做错了什么。

Thanks谢谢

You use the constructor-forwarding version of io::stream , which construct a tee-stream itself and forward all arguments to that.您使用io::stream构造函数转发版本,它自己构造一个 tee-stream 并将所有 arguments 转发到该版本。 C++03 has only limited capabilities when it comes to forwarding arguments to functions (amount of overloads needed easily grow exponentially).在将 arguments 转发给函数时,C++03 的功能有限(所需的重载量很容易成倍增长)。 It ( io::stream ) makes the following restrictions:它( io::stream )做出以下限制:

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.这些成员中的每一个都构造了一个 stream 的实例,并将其与从给定的 arguments 列表构造的设备 T 的实例相关联。 The T constructors involved must take all arguments by value or const reference.涉及的 T 构造函数必须通过值或 const 引用获取所有 arguments。

Well, but the tee_device constructor says好吧,但是tee_device构造函数说

Constructs an instance of tee_device based on the given pair of Sinks.根据给定的一对接收器构造 tee_device 的实例。 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.如果相应的模板参数是 stream 或 stream 缓冲区类型,则每个 function 参数都是非常量引用,否则是常量引用。

That won't work, of course.这当然行不通。 io::stream provides another constructor that takes a T as first argument. io::stream提供了另一个将T作为第一个参数的构造函数。 This works here (Compiles, at least. The assertion fails, though. I've not worked with boost::iostreams so i can't help with that)这在这里有效(至少编译。但是断言失败了。我没有使用boost::iostreams所以我无能为力)

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.编辑:在调用flush()或流<< std::flush后,断言通过。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM