使用boost :: iostreams :: tee_device?

rlb*_*ond 7 c++ iostream stream boost-iostreams

有人能帮我吗?

我想尝试做以下事情:

#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());
Run Code Online (Sandbox Code Playgroud)

但它不会在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
Run Code Online (Sandbox Code Playgroud)

有没有人得到这个工作?我知道我可以自己上课去做,但我想知道我做错了什么.

谢谢

Joh*_*itb 11

您可以使用构造函数转发版本io::stream,它构建一个三通流本身和所有参数转发了这一点.在向函数转发参数时,C++ 03只具有有限的功能(所需的过载量很容易成指数增长).它(io::stream)具有以下限制:

这些成员中的每一个构造流的实例并将其与从给定的参数列表构造的Device T的实例相关联.涉及的T构造函数必须通过值或const引用获取所有参数.

好吧,但tee_device构造函数说

根据给定的Sink对构造一个tee_device实例.如果相应的模板参数是流或流缓冲区类型,则每个函数参数都是非const引用,否则是const引用.

当然,那不行.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());
Run Code Online (Sandbox Code Playgroud)

编辑:调用flush()或流式传输后<< std::flush,断言通过.