title: Make a C++ initializer automatically detect union member?
body: <p>OK, the standard still says that brace-initializing a union initializes only the first member.  I suppose this puts this question more in the "Wouldn't it be nice if...?" category.</p>
  
  <p>Does it have to be this way? After all, we now have automatic type detection for other types.</p>
  
  <pre><code>auto x = 3; // int
  auto s = "foo"; // char *
  auto w = L"Foo"; // wchar_t *
  </code></pre>
  
  <p>So why not with a union? Given:</p>
  
  <pre><code>struct init 
  {
   int t;
   union {
     long long x;
     char *s;
     wchar_t *w;
   };
  };
  </code></pre>
  
  <p>Currently you can only brace-initialize <code>init::x</code> (with an <code>int</code>), not <code>s</code> or <code>w</code>. </p>
  
  <p>Automatic type detection might be extended so that the union member initialized is selected based on the type of the initializer:</p>
  
  <pre><code>auto init initial_data [] = {
   { 0, 3 },      // initializes x, brace elision
   { 1, "foo" },  // initializes s 
   { 2, L"Foo" }  // initializes w
  };
  </code></pre>
  
  <p>(of course, this should not compile in the current standard.)</p>
  
  <p>This would make it possible to put all of the <code>initial_data</code> in a single initializer list. (I suspect the auto keyword would have to go somewhere else).  </p>
  
  <p>Is there something making this a Bad Idea (beyond "nobody's thought of it yet" or "it's too hard to implement")?  Currently you have to do something horrendous like:</p>
  
  <pre><code>#define RI(X) reinterpret_cast&lt;long long&gt;(X)
  const init initial_data[] = {
  { 0, 3 },      // initializes x, brace elision
  { 1, RI("foo") },  // initializes s 
  { 2, RI(L"Foo") }  // initializes w
  };
  </code></pre>
  
  <p>I wouldn't want to go through these contortions, except that a <code>std::exception</code> must be initialized with a <code>std::string</code> (i.e. based on <code>char</code>), so I can't just have an initializer list with all <code>wchar_t*</code>.  And I'd really like all of those initializers in the same place. </p>