title: c++ static_cast and references
body: <pre><code>struct A{};
  struct B : A{};
  
  int main()
  {
      A a;
      A&amp; a_ref = a;
  
      static_cast&lt;B&gt;(a); // *1
      static_cast&lt;B&amp;&gt;(a_ref); // *2
  
      return 0;
  }
  </code></pre>
  
  <p>(*1) produces an error and i understand why. (*2) compiles fine, but why? And, as long as it compiles and suppose <code>B</code> contains some attributes, what if i cast <code>a_ref</code> to <code>B&amp;</code> and then try to access the attributes? I think i will have an run-time error or something. </p>
  
  <p>So, as i can see, there is a situation which leads to crash and there are no ways to avoid it, unlike with <code>dynamic_cast</code> where one can check the result of casting for null or put code in a <code>try-catch</code> region. How do i have to deal with such situation where i need to <em>cast references</em> and be sure that i really get the right references.</p>