I'd like to create tuple of type which depends on the name of that type.
Maybe example would clarify my problem:
//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), ...);
}
EDIT: I've found nice way, template recursion. Prob开发者_如何学编程lem solved.
typedef typename oost::tuples::element<0,GT>::type tt0;
...
if(tt3==null_type)
you cant check types likes this, use mpl::if_
instead
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), ...);
are you trying to remove null_type? Just use boost::fusion::remove
.
Here is another solution:
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));
}
similary for other case.
notice: boost::enable_if
, boost::is_same
精彩评论