title: Casting pointers and the ternary ?: operator. Have I reinvented the wheel?
body: <p>The last line of this code fails to compile with <code>castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast</code></p>
  
  <p>A really smart compiler could have no difficulty because both can be safely casted to <code>B*</code> (the base class). I'm reluctant to use static_cast and dynamic_cast and so on - I'm worried that I'll mix up the classes someday and get undefined behaviour. That's why I created the up_cast template. This template does the bare minimum in allowed conversion. Is there a simpler way? There are other workarounds, but I can't help but think that there's something even simpler and safer that I could use?</p>
  
  <pre><code>struct B{ };
  struct D1 : public B{ };
  struct D2 : public B{ };
  
  template&lt;typename T,typename V&gt;
  T up_cast(V x) {
          return x;
  }
  int main() {
          bool boolean_expression = true;
          B * b;
          b = new D1;
          b = new D2;
          b = boolean_expression ? up_cast&lt;B*&gt;(new D1) : up_cast&lt;B*&gt;(new D2);
          b = boolean_expression ? new D1 : new D2;
  }
  </code></pre>
  
  <p>g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3</p>
  
  <p><strong>Update</strong> changed name from <code>implicit_cast</code> to <code>up_cast</code> as per @Konrad's answer</p>