title: Conditional invocation of constructor
body: <p>Suppose some class <code>foo</code> has two custom constructors, say <code>foo::foo(bar const &amp;)</code> and <code>foo::foo(baz const &amp;)</code>. What would be considered good style for invoking either one depending on some condition. For example:</p>
  
  <pre><code>bar a;
  baz b;
  foo my_foo;
  if (...) {
      my_foo = foo(a);
  } else {
      my_foo = foo(b);
  }
  </code></pre>
  
  <p>would require <code>foo</code> to be default constructible (which is not very sensible in the concrete case I have in mind) and is essentially wasting time (and memory) on the creation (and deletion) of the default-constructed temporary object. Because of scope, one cannot declare <code>my_foo</code> inside the if or else blocks.</p>
  
  <p>An alternative would be to use a pointer and construct with <code>new</code>. This would be less efficient (indirection, heap allocation) and potentially unsafe (no guarantee pointer isn't dangling; need to <code>delete</code>, though the latter may be taken care of by using <code>std::unique_ptr</code>).</p>
  
  <p>I found one way to do it:</p>
  
  <pre><code>foo my_foo = (...) ? foo(a) : foo(b);
  </code></pre>
  
  <p>which works because the ternary operator is guaranteed to be exhaustive and thus scope is not an issue.</p>
  
  <p>I'm not a big fan of the ternary operator and might want to do some other things in the conditional blocks before invoking the ctors. Is there an elegant way to achieve the same thing with the traditional if-else syntax?</p>