Constructing one concrete boost::tuple type using another

166 views Asked by At

Given:

typedef boost::tuple< T1, T2, T3, ..., Tn > Tuple_Tn

where the types T1, ... Tn are all defined,

And given type T_another, I'd like to define a new tuple type:

typedef boost::tuple< T1, T2, T3, ..., Tn, T_another > Tuple_T_plus_1

But here is my problem: in the place where I want to define it I only have access to types Tuple_Tn and T_another.

In other words, Is it possible to define Tuple_T_plus_1 in terms of Tuple_Tn and T_another only?

1

There are 1 answers

1
Luc Touraille On

I'm not sure there is such a feature in Boost.Tuple, perhaps Boost.Fusion would be more suited for your needs.

However, if you have a compiler supporting C++11 variadic templates, you can switch to std::tuple and write a small metafunction for appending a type to an existing tuple:

template <typename Container, typename T>
struct push_back;

template <template <typename...> class Container, typename T, typename... Args>
struct push_back<Container<Args...>, T>
{
    typedef Container<Args..., T> type;
};

typedef std::tuple<int, double> myTuple;
typedef push_back<myTuple, bool>::type myOtherTuple;

myOtherTuple(1, 0.0, true);

The same thing can be achieved for boost::tuple, but would be much more tedious to write.