繁体   English   中英

使用 boost::iostreams::tee_device?

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

有人能帮我吗?

我正在尝试执行以下操作:

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

但它不会在 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

有没有人让这个工作? 我知道我可以制作自己的 class 来做到这一点,但我想知道我做错了什么。

谢谢

您使用io::stream构造函数转发版本,它自己构造一个 tee-stream 并将所有 arguments 转发到该版本。 在将 arguments 转发给函数时,C++03 的功能有限(所需的重载量很容易成倍增长)。 它( io::stream )做出以下限制:

这些成员中的每一个都构造了一个 stream 的实例,并将其与从给定的 arguments 列表构造的设备 T 的实例相关联。 涉及的 T 构造函数必须通过值或 const 引用获取所有 arguments。

好吧,但是tee_device构造函数说

根据给定的一对接收器构造 tee_device 的实例。 如果相应的模板参数是 stream 或 stream 缓冲区类型,则每个 function 参数都是非常量引用,否则是常量引用。

这当然行不通。 io::stream提供了另一个将T作为第一个参数的构造函数。 这在这里有效(至少编译。但是断言失败了。我没有使用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());

编辑:在调用flush()或流<< std::flush后,断言通过。

可能您需要像这样设置它:

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