Fairly straightforward recursive formulation:
template<typename T, unsigned N, typename... REST>
struct generate_tuple_type
{
typedef typename generate_tuple_type<T, N-1, T, REST...>::type type;
};
template<typename T, typename... REST>
struct generate_tuple_type<T, 0, REST...>
{
typedef std::tuple<REST...> type;
};
Live example
[Update]
OK, so I was only thinking about modest values of N
. The following formulation is more complex, but also significantly faster and less compiler-crushing for large arguments.
#include <tuple>
template<typename /*LEFT_TUPLE*/, typename /*RIGHT_TUPLE*/>
struct join_tuples
{
};
template<typename... LEFT, typename... RIGHT>
struct join_tuples<std::tuple<LEFT...>, std::tuple<RIGHT...>>
{
typedef std::tuple<LEFT..., RIGHT...> type;
};
template<typename T, unsigned N>
struct generate_tuple_type
{
typedef typename generate_tuple_type<T, N/2>::type left;
typedef typename generate_tuple_type<T, N/2 + N%2>::type right;
typedef typename join_tuples<left, right>::type type;
};
template<typename T>
struct generate_tuple_type<T, 1>
{
typedef std::tuple<T> type;
};
template<typename T>
struct generate_tuple_type<T, 0>
{
typedef std::tuple<> type;
};
int main()
{
using gen_tuple_t = generate_tuple_type<int, 30000>::type;
static_assert( std::tuple_size<gen_tuple_t>::value == 30000, "wrong size" );
}
Live example
This version performs at most 2*log(N)+1 template instantiations, assuming your compiler memoizes them. Proof left as an exercise for the reader.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…