2011-01-12 25 views
0

我想創建取決於該類型的名稱的類型的元組。如何創建一個取決於元組類型名的boost元組?

也許例子就是澄清我的問題:

//T would be boost::tuples::tuple<int, int, ...> 
template <typename T> 
T generate() 
{ 
    typedef T GT; 
    typedef boost::tuples::element<0,GT>::type tt0; 
    typedef boost::tuples::element<1,GT>::type tt1; 
    typedef boost::tuples::element<2,GT>::type tt2; 
    if(tt3==null_type) 
     return boost::tuples::make_tuple<tt0,tt1>(static_cast<tt0>(1), static_cast<tt1>(2)); 
    else 
     return boost::tuples::make_tuple<tt0, tt1, tt2>(static_cast<tt0>(1), static_cast<tt1>(2), ...); 
} 

編輯: 我找到很好的方式,模板遞歸。 問題解決了。

+0

,並且這些錯誤是...? – 2011-01-12 01:36:10

+0

`typename`更自由的應用程序可以讓你得到你想要的。 – 2011-01-12 01:47:20

回答

2
typedef typename oost::tuples::element<0,GT>::type tt0; 

...

if(tt3==null_type) 

你不能檢查類型喜歡這一點,使用mpl::if_代替

 return boost::tuples::make_tuple<tt0,tt1>(static_cast<tt0>(1), static_cast<tt1>(2)); 
    else 
     return boost::tuples::make_tuple<tt0, tt1, tt2>(static_cast<tt0>(1), static_cast<tt1>(2), ...); 

你想刪除null_type?只需使用boost::fusion::remove

下面是另一種解決方案:

template <typename T> 
tuple<tt0,tt1> 
generate(typename enable_if<is_same<tt3, null_type> >::type* = 0) { 
    typedef typename boost::tuples::element<0,T>::type tt0; 
    typedef typename boost::tuples::element<1,T>::type tt1; 
    return make_tuple(tt0(1), tt1(1)); 
} 

與之相似用於其它情況。

通知:boost::enable_ifboost::is_same

相關問題