0

我的目標是將數據發送到多個流。這可以通過使用boost :: tee來實現。但是我想用variadic模板編寫一個封裝器來使用多個流。如何在可變參數模板中聲明「隱式轉換」?

問題是我需要從後代結構到祖先結構的隱式轉換。或類似的東西。

#include <boost/iostreams/tee.hpp> 
#include <boost/iostreams/stream.hpp> 
#include <fstream> 
#include <iostream> 
using namespace std; 
namespace bio = boost::iostreams; 
using bio::tee_device; 
using bio::stream; 

template<typename ... Ts> 
struct pu; 

template<typename T1,typename T2> 
struct pu<T1,T2>:public stream<tee_device<T1,T2>> 
{ 
typedef stream<tee_device<T1,T2>> base_type; 
operator base_type() { return static_cast<base_type&>(*this); } 
base_type& base = static_cast<base_type&>(*this); 
pu(T1& t1, T2& t2): base_type(tee_device<T1,T2>(t1,t2)) {} 
}; 

template<typename T1,typename T2,typename T3,typename ... Ts> 
struct pu<T1,T2,T3,Ts...> : public stream<tee_device<T1,pu<T2, T3, Ts ...>>> 
{ 
typedef stream<tee_device<T1,pu<T2, T3, Ts ...>>> base_type; 
operator base_type() { return static_cast<base_type&>(*this); } 
pu(T1& t1, T2& t2, T3& t3, Ts& ... ts) : base_type(t2,t3,ts...){} 
}; 

int main() 
{ 
pu<ostream,ostream> hT(cout,cout); hT<<"2"; 
pu<ostream,ostream,ostream> hR(cout,cout,cout); hR<<"3"; 
return 0; 
} 

該錯誤是

..\boost_1_56_0\boost\iostreams\detail\forward.hpp|73|error: no matching function for call to 
'boost::iostreams::tee_device<std::basic_ostream<char>, pu<std::basic_ostream<char, std::char_traits<char> >, std::basic_ostream<char, std::char_traits<char> > > >::tee_device(std::basic_ostream<char>&, const std::basic_ostream<char>&)'| 

預期的輸出是 「22333」。 (我有「22」,但沒有「333」。也就是說,它是無主的第二線運行良好)

換句話說,我需要從

template<typename T1,typename T2,typename T3,typename ... Ts> 

轉換

stream<tee_device<T1,pu<T2, T3, Ts ...>>> 

模板內。

謝謝!

p.s. (這是我的第一篇文章)& &(我不是母語)

回答

1

你已經擁有你問,因爲類類型已隱式轉換爲它們的公共基本類型的轉換,但是這不是你所需要的解決錯誤。

中的tu一個可變專業化base_type構造函數的參數是錯誤的:

pu(T1& t1, T2& t2, T3& t3, Ts&... ts) : base_type(t2, t3, ts...) {} 

你可以代替試試這個:

pu(T1& t1, T2& t2, T3& t3, Ts&... ts) : base_type(t1, pu<T2, T3, Ts...>(t2, t3, ts...)) {} 

但是,這不會因爲工作,根據documentation,如果tee_device構造函數的參數是流,那麼它們必須綁定到對非const的引用,所以它們必須是左值。第一個參數t1符合該標準,但第二個參數pu沒有。

以下解決方案使用多重繼承合成一個左pu

template <typename T> 
struct hold 
{ 
    T held; 
    template <typename... Ts> hold(Ts&&... vs) : held(std::forward<Ts>(vs)...) {} 
}; 

template<typename...> 
struct pu; 

template<typename T1, typename T2> 
struct pu<T1, T2> : public stream<tee_device<T1, T2>> 
{ 
    typedef stream<tee_device<T1, T2>> base_type; 
    pu(T1& t1, T2& t2): base_type(tee_device<T1, T2>(t1, t2)) {} 
}; 

template<typename T1, typename T2, typename T3, typename... Ts> 
struct pu<T1, T2, T3, Ts...> : private hold<pu<T2, T3, Ts...>> 
          , public stream<tee_device<T1, pu<T2, T3, Ts...>>> 
{ 
    typedef stream<tee_device<T1, pu<T2, T3, Ts...>>> base_type; 
    pu(T1& t1, T2& t2, T3& t3, Ts&... ts) : hold<pu<T2, T3, Ts...>>(t2, t3, ts...) 
              , base_type(t1, this->held) {} 
}; 
+0

感謝名單,但我有絕對相同的問題吧。 ** \ boost \ iostreams \ detail \ forward.hpp | 73 |錯誤:沒有匹配函數調用'boost :: iostreams :: tee_device ,pu > std :: basic_ostream >>> :: tee_device(std :: basic_ostream &,const pu >,std :: basic_ostream >>&)'| ** – ged

+0

@GeorgeDunaev編輯。 – Oktalist