使用 boost::iostreams::tee_device?

发布于 2024-07-16 01:41:37 字数 820 浏览 13 评论 0原文

有人能帮我吗?

我正在尝试做类似以下的事情:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒲公英的约定 2024-07-23 01:41:37

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

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

好吧,但是 tee_device 构造函数说

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

当然,那是行不通的。 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,断言通过。

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:

Each of these members constructs an instance of stream and associates it with an instance of the Device T constructed from the given lists of arguments. The T constructors involved must take all arguments by value or const reference.

Well, but the tee_device constructor says

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.

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

Edit: After calling flush() or streaming << std::flush, the assertion passes.

仙女山的月亮 2024-07-23 01:41:37

可能你需要这样设置:

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

Probably you need to set it up like this:

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);
若言繁花未落 2024-07-23 01:41:37

使用 std::ref 传递不可复制的接收器。

Tee my_split(std::ref(ss1), std::ref(ss2));

Use std::ref to pass non-copyable sinks.

Tee my_split(std::ref(ss1), std::ref(ss2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文