簡體   English   中英

使用 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 中編譯:

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

有沒有人讓這個工作? 我知道我可以制作自己的 class 來做到這一點,但我想知道我做錯了什么。

謝謝

您使用io::stream構造函數轉發版本,它自己構造一個 tee-stream 並將所有 arguments 轉發到該版本。 在將 arguments 轉發給函數時,C++03 的功能有限(所需的重載量很容易成倍增長)。 它( io::stream )做出以下限制:

這些成員中的每一個都構造了一個 stream 的實例,並將其與從給定的 arguments 列表構造的設備 T 的實例相關聯。 涉及的 T 構造函數必須通過值或 const 引用獲取所有 arguments。

好吧,但是tee_device構造函數說

根據給定的一對接收器構造 tee_device 的實例。 如果相應的模板參數是 stream 或 stream 緩沖區類型,則每個 function 參數都是非常量引用,否則是常量引用。

這當然行不通。 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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM