【问题标题】:Using boost::iostreams::tee_device?使用 boost::iostreams::tee_device?
【发布时间】:2010-10-14 19:16:19
【问题描述】:

有人可以帮我吗?

我正在尝试执行以下操作:

#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

有人让这个工作吗?我知道我可以让自己的班级来做,但我想知道我做错了什么。

谢谢

【问题讨论】:

    标签: c++ stream iostream boost-iostreams


    【解决方案1】:

    您使用io::streamconstructor-forwarding version,它自己构造一个 tee-stream 并将所有参数转发给它。在将参数转发给函数时,C++03 的功能有限(所需的重载数量很容易成倍增长)。它 (io::stream) 做了以下限制:

    这些成员中的每一个都构造了一个流实例,并将它与根据给定参数列表构造的设备 T 的实例相关联。 所涉及的 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() 或流式传输&lt;&lt; std::flush 后,断言通过。

    【讨论】:

    • 哇...在文档中没有看到。很棒的工作,非常感谢!
    【解决方案2】:

    可能你需要这样设置:

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

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2012-06-06
      • 2018-08-28
      • 2016-11-20
      • 2012-09-14
      相关资源
      最近更新 更多