使用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中编译:
But it won't compile in 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.
感谢
推荐答案
您可以使用 io :: stream 的通用%5Fstreams.html#stream%5Fforwarding%5Fconstructors> constructor-forwarding版本论据。 C ++ 03只有有限的功能,当涉及到转发函数的参数(需要的重载量需要容易地成指数增长)。它( io :: stream
)有以下限制:
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:
成员构造流的实例,并将其与从给定的参数列表构建的Device T的实例相关联。
好吧,但是<$> c $ c> tee_device 构造函数
根据给定的Sink对构造tee_device的实例。如果相应的模板参数是流或流缓冲区类型,则每个函数参数是非常量引用,否则为const引用。
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.
当然不会工作。 io :: stream
提供另一个构造函数,它使用 T
作为第一个参数。这个工作在这里(至少编译,断言,虽然,我没有使用 boost :: iostreams
,所以我不能帮助)
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());
编辑:调用 flush()
流<< std :: flush
,断言通过。
After calling flush()
or streaming << std::flush
, the assertion passes.
这篇关于使用boost :: iostreams :: tee_device?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!