title: Conditionally assign type to variable in c++
body: <p>apologies if this has been asked before, I couldn't quite find exactly what I was looking for.</p>
  <p>Essentially based on some condition I want to assign a variable A as either an int or a double.</p>
  <p>Originally I thought I could do something like</p>
  <pre><code>auto a = (condition) ? 3 : 5.1;
  </code></pre>
  <p>However I realised the the int 3 is promoted to a double and so a becomes a double type regardless.</p>
  <p>I understand c++ is strongly typed, but I would have thought there might be some neat trick to achieve this.</p>
  <p>Thanks.</p>
  <p>*** Edit / Clarification ***</p>
  <p>I am not sure if the condition is known at compile time. I think not.</p>
  <p>The way in which I would like to use the assigned variable is for example assuming I have a STL container <code>std::vector&lt;std::variant&lt;int,double&gt;&gt;</code></p>
  <p>Then I would like to assign a variable as either an int or double:
  e.g.</p>
  <pre><code>bool check_state(std::variant&lt;int, double&gt; const&amp; v) {
      try {
         std::get&lt;double&gt;(v);
      } catch (...) {
         return true;
      }
      return false;
  }
  
  auto a = check_state(stack[stack.size() -1]) ? std::get&lt;int&gt;(stack[stack.size() -1]) : std::get&lt;double&gt;(stack[stack.size() -1]);
  </code></pre>