title: constexpr initialization of array to sort contents
body: <p>This is a bit of a puzzle rather than a real-world problem, but I've gotten into a situation where I want to be able to write something that behaves exactly like</p>
  
  <pre><code>template&lt;int N&gt;
  struct SortMyElements {
      int data[N];
  
      template&lt;typename... TT&gt;
      SortMyElements(TT... tt) : data{ tt... }
      {
          std::sort(data, data+N);
      }
  };
  
  int main() {
      SortMyElements&lt;5&gt; se(1,4,2,5,3);
      int se_reference[5] = {1,2,3,4,5};
      assert(memcmp(se.data, se_reference, sizeof se.data) == 0);
  }
  </code></pre>
  
  <p>except that I want the <code>SortMyElements</code> constructor to be <strong><code>constexpr</code></strong>.</p>
  
  <p>Obviously this is possible for fixed <code>N</code>; for example, I can specialize</p>
  
  <pre><code>template&lt;&gt;
  struct SortMyElements&lt;1&gt; {
      int data[1];
      constexpr SortMyElements(int x) : data{ x } {}
  };
  
  
  template&lt;&gt;
  struct SortMyElements&lt;2&gt; {
      int data[2];
      constexpr SortMyElements(int x, int y) : data{ x&gt;y?y:x, x&gt;y?x:y } {}
  };
  </code></pre>
  
  <p>But how do I generalize this into something that will work for <em>any</em> <code>N</code>?</p>
  
  <hr>
  
  <p>Please notice that the array elements have to come from the actual values of the arguments, not from template non-type arguments; my elements come from <code>constexpr</code> expressions that, despite being evaluated at compile-time, reside firmly inside the "value system", rather than the "type system". (For example, <a href="http://www.boost.org/doc/libs/1_51_0/libs/mpl/doc/refmanual/sort.html" rel="noreferrer">Boost.MPL's <code>sort</code></a> works strictly within the "type system".)</p>
  
  <p>I've posted a working "answer", but it's too inefficient to work for <code>N &gt; 6</code>. I'd like to use this with <code>2 &lt; N &lt; 50</code> or thereabouts.</p>
  
  <p>(P.S.— Actually what I'd really like to do is shuffle all the zeroes in an array to the end of the array and pack the nonzero values toward the front, which might be easier than full-on sorting; but I figure sorting is easier to describe. Feel free to tackle the "shuffle zeroes" problem instead of sorting.)</p>