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