有人能帮助我吗?

我试图做类似以下内容:

#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

有没有人得到这个工作?我知道我可以做我自己的类来做到这一点,但我想知道我做错了。

由于

有帮助吗?

解决方案

您使用构造函数的转发io::stream的版本,其构造三通流本身和所有参数转发了这一点。 C ++ 03,当涉及到转发函数的参数(容易成倍增长所需的过载量)只有有限的能力。它(io::stream)使以下限制:

  

每个这些部件的构造流并且将其相关联的实例与从参数给定的列表构造的设备T的一个实例。 涉及必须通过值或const引用采取所有参数的T的构造函数。

好了,但tee_device构造说

  

构造基于给定的一对汇的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());

编辑:主叫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归因
不隶属于 StackOverflow
scroll top