title: Passing arguments to "array-like" container constructor
body: <h2>Background</h2>
  
  <p>I'm  working with an embedded platform with the following restrictions:</p>
  
  <ul>
  <li>No heap</li>
  <li>No Boost libraries</li>
  <li>C++11 <strong>is</strong> supported</li>
  </ul>
  
  <p>I've dealt with the following problem a few times in the past:</p>
  
  <blockquote>
    <p>Create an array of class type T, where T has no default constructor</p>
  </blockquote>
  
  <p>The project only recently added C++11 support, and up until now I've been using ad-hoc solutions every time I had to  deal with this. Now that C++11 is available, I thought I'd try to make a more general solution.</p>
  
  <h2>Solution Attempt</h2>
  
  <p>I copied <a href="http://en.cppreference.com/w/cpp/types/aligned_storage" rel="nofollow">an example of std::aligned_storage</a> to come up with the framework for my array type. The result looks like this:</p>
  
  <pre><code>#include &lt;type_traits&gt;
  
  template&lt;class T, size_t N&gt;
  class Array {
    // Provide aligned storage for N objects of type T
    typename std::aligned_storage&lt;sizeof(T), alignof(T)&gt;::type data[N];
  
  public:
    // Build N objects of type T in the aligned storage using default CTORs
    Array()
    {
      for(auto index = 0; index &lt; N; ++index)
        new(data + index) T();
    }
  
    const T&amp; operator[](size_t pos) const
    {
      return *reinterpret_cast&lt;const T*&gt;(data + pos);
    }
  
    // Other methods consistent with std::array API go here
  };
  </code></pre>
  
  <p>This is a basic type - <code>Array&lt;T,N&gt;</code> only compiles if <code>T</code> is default-constructible. I'm not very familiar with template parameter packing, but looking at some examples led me to the following:</p>
  
  <pre><code>template&lt;typename ...Args&gt;
  Array(Args&amp;&amp;... args) 
  {
    for(auto index = 0; index &lt; N; ++index)
      new(data + index) T(args...);
  }
  </code></pre>
  
  <p>This was definitely a step in the right direction. <code>Array&lt;T,N&gt;</code> now compiles if passed arguments matching a constructor of <code>T</code>. </p>
  
  <p>My only remaining problem is constructing an <code>Array&lt;T,N&gt;</code> where different elements in the array have different constructor arguments. I figured I could split this into two cases:</p>
  
  <h3>1 - User Specifies Arguments</h3>
  
  <p>Here's my stab at the CTOR:</p>
  
  <pre><code>template&lt;typename U&gt;
  Array(std::initializer_list&lt;U&gt; initializers)
  {
    // Need to handle mismatch in size between arg and array
    size_t index = 0;
    for(auto arg : initializers) {
      new(data + index) T(arg);
      index++;
    }
  }
  </code></pre>
  
  <p>This seems to work fine, aside from needing to handle a dimension mismatch between the array and initializer list, but there are a number of ways to deal with that that aren't important. Here's an example:</p>
  
  <pre><code>struct Foo {
    explicit Foo(int i) {}
  };
  
  void bar() {
    // foos[0] == Foo(0)
    // foos[1] == Foo(1)
    // ..etc
    Array&lt;Foo,10&gt; foos {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  }
  </code></pre>
  
  <h3>2 - Arguments Follow Pattern</h3>
  
  <p>In my previous example, <code>foos</code> is initialized with an incrementing list, similar to <code>std::iota</code>. Ideally I'd like to support something like the following, where <code>range(int)</code> returns SOMETHING that can initialize the array. </p>
  
  <pre><code>// One of these should initialize foos with parameters returned by range(10)
  Array&lt;Foo,10&gt; foosA = range(10);
  Array&lt;Foo,10&gt; foosB {range(10)};
  Array&lt;Foo,10&gt; foosC = {range(10)};
  Array&lt;Foo,10&gt; foosD(range(10));
  </code></pre>
  
  <p>Googling has shown me that <code>std::initializer_list</code> isn't a "normal" container, so I don't think there's any way for me to make <code>range(int)</code> return a <code>std::initializer_list</code> depending on the function parameter.</p>
  
  <p>Again, there are a few options here:</p>
  
  <ul>
  <li>Parameters specified at run-time (function return?)</li>
  <li>Parameters specified at compile-time (<code>constexpr</code> function return? templates?)</li>
  </ul>
  
  <h2>Questions</h2>
  
  <ol>
  <li>Are there any issues with this solution so far?</li>
  <li>Does anyone have a suggestion to generate constructor parameters? I can't think of a solution at runtime or compile-time other than hard-coding an <code>std::initializer_list</code>, so any ideas are welcome.</li>
  </ol>