I'm going to recycle the idea from @Smeeheey 's answer to a different question which I asked: C++ parameter pack, constrained to have instances of a single type?
The connection is, your question hasn't got much to do with tuple
, it's really,
How do I make a parameter pack which contains the same type repeated some number of times?
And the most straightforward answer is, use std::make_index_sequence
and a pack expansion:
#include <tuple>
#include <utility>
template <typename T, unsigned n>
struct repeat_tuple {
template <unsigned>
struct blah {
using type = T;
};
template <typename IS>
struct helper;
template <unsigned ... Is>
struct helper<std::integer_sequence<unsigned, Is...>> {
using type = std::tuple<typename blah<Is>::type...>;
};
using type = typename helper<std::make_integer_sequence<unsigned, n>>::type;
};
template <typename T, unsigned n>
using repeat_tuple_t = typename repeat_tuple<T, n>::type;
static_assert(std::is_same<repeat_tuple_t<int, 3>, std::tuple<int, int, int>>::value, "");
int main() {}
This is ultimately the same as W.F.'s answer, but maybe a little terser. Edit: I guess he is backporting C++14 traits in his answer.
This approach works for things besides tuple as well, unlike the tuple_cat
approach.
In fact I guess std::tuple
could be a template template parameter here ...
:evil eyes: