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?
Although this is totally doable with variadic templates and
std::tuple
, the best solution for what you want I think is to just usestd::array
. If you simply want a container with N instances of T, thestd::array
signature is alreadytemplate <typename T, std::size_t N> class array
. I think it fits your need exactly.Having said that, if you really want
std::tuple
for some reason you can do it like so:Note: If you don't have access to C++11 but do have access to boost,
boost::array
andboost::tuples::tuple
will do fine in lieu ofstd::array
andstd::tuple
.