使用 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
有人让它工作吗? 我知道我可以让我自己的课程来做到这一点,但我想知道我做错了什么。
谢谢
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:
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.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用构造函数转发版本,它本身构造一个 tee-stream 并将所有参数转发给它。 在将参数转发给函数时,C++03 的功能有限(所需的重载量很容易呈指数增长)。 它(
io::stream
)做出以下限制:好吧,但是
tee_device
构造函数说当然,那是行不通的。
io::stream
提供了另一个采用T
作为第一个参数的构造函数。 这在这里有效(至少可以编译。不过,断言失败了。我没有使用过 boost::iostreams 所以我无能为力)编辑:调用后flush( ) 或流式
<< std::flush
,断言通过。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:Well, but the
tee_device
constructor saysThat won't work, of course.
io::stream
provides another constructor that takes aT
as first argument. This works here (Compiles, at least. The assertion fails, though. I've not worked withboost::iostreams
so i can't help with that)Edit: After calling
flush()
or streaming<< std::flush
, the assertion passes.可能你需要这样设置:
Probably you need to set it up like this:
使用 std::ref 传递不可复制的接收器。
Use
std::ref
to pass non-copyable sinks.