How to create a boost::tuple with the specified number of elements (of the same type)?

Viewed 1002

Assume that I have the following class definition:

template <unsigned int N>
class foo
{
    boost::tuples::tuple<...> bar;
};

Given the compile-time constant N, I would like to expand the type of bar to be a tuple that holds N elements of a specified type. That is, the type of foo<2>::bar would be boost::tuples::tuple<T, T>. I'm guessing that I can use Boost.MPL for this, but I haven't figured out the exact sequence yet. I think I could do:

template <typename T, int N>
struct type_repeater
{
    typedef typename boost::mpl::fold<
        boost::mpl::range_c<T, 0, N>,
        boost::mpl::vector<>,
        boost::mpl::push_back<_1, T>
    >::type type;
};

So then for instance type_repeater<T, 2>::type would be equivalent to boost::mpl::vector<T, T>. I'm just not sure how/if I can take that type list and inject it into the argument list of a tuple, like I want. Is this possible?

3 Answers
Related