Variadic template for concisely defining a tuple whose components are all the same type C++11 provides a std::tuple template, but it requires the types of all the fields to be listed individually, which is inconvenient if there are many fields and they're all the same type.  If I want a tuple of, say, 10 integers, instead of writing std::tuple&lt;int, int, int, int, int, int, int, int, int, int&gt;, I'd like to be able to just write something like tuple_of&lt;10, int&gt;.

To that end, I've written a template that expands a number into that many copies of a type:

template &lt;unsigned int n, typename T&gt;
struct tuple_of {
private:

  // Adds another copy of T to the list
  template &lt;unsigned int m, typename... Ts&gt;
  struct tuple_builder {
    using type = typename tuple_builder&lt;m-1, T, Ts...&gt;::type;
  };

  // Makes a tuple out of all the copies of T that have been listed
  template &lt;typename... Ts&gt;
  struct tuple_builder&lt;0, Ts...&gt; {
    using type = std::tuple&lt;Ts...&gt;;
  };

public:

  tuple_of() = delete;  // Prevent accidental instantiation

  using type = typename tuple_builder&lt;n, T&gt;::type;
};

// Convenience alias
template &lt;unsigned int n, typename T&gt;
using tuple_of_t = typename tuple_of&lt;n, T&gt;::type;


Now you can write either tuple_of&lt;10, int&gt;::type or tuple_of_t&lt;10, int&gt;.

This works, but it seems a little awkward.  A few things in particular bother me:


I don't really like having to write either ::type or the _t suffix of the convenience alias; I'd rather just write tuple_of&lt;10, int&gt;.  I've followed the pattern established by the templates in the standard &lt;type_traits&gt; header — e.g. std::result_of_t&lt;T&gt; is an alias for std::result_of&lt;T&gt;::type — but tuple_of isn't a type trait so I don't know if I should be following that convention.
I've made the template a struct because that seems to be the norm for templates used in metaprogramming, but it contains a private helper template that I don't really want to expose, and it's a little weird to have private stuff in a struct.
This is the first variadic template I've written, so I'm not sure that my tuple_builder is the most elegant way to build a list of n copies of a type.
While testing, I accidentally created an instance of the template type itself, rather than the tuple type it produces, by writing tuple_of&lt;10, int&gt; instead of tuple_of&lt;10, int&gt;::type.  To avoid that mistake in the future, I deleted the constructor.  I don't know whether that's something templates like this "should" do; the templates in &lt;type_traits&gt; don't have deleted constructors.


Can this template be improved?
As nwp pointed out in a comment, std::array is a better choice for this.  I didn't need to write this template at all.