[英]How can I create a boost tuple which depends on the typename of tuple?
我想創建類型的元組,具體取決於該類型的名稱。
也許例子可以澄清我的問題:
//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), ...);
}
編輯:我發現了不錯的方法, 模板遞歸 。 問題解決了。
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_if
, boost::is_same
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.