title: Why am I getting the error "A is an inaccessible base of B" when using dynamic_cast and templates?
body: <p>Why is the call to <code>f</code> not resolving to the first function overload? I get the error:</p>
  
  <blockquote>
    <p><code>source.cpp: In function 'int main()':</code><br>
    <code>source.cpp:12:31: error: 'A' is an inaccessible base of 'B'</code></p>
  </blockquote>
  
  <pre><code>class A {}; class B : A {};
  
  void f(const A &amp;)  { std::cout &lt;&lt; "const A &amp;"; }
  template &lt;typename T&gt; void f(T) { std::cout &lt;&lt; "Generic";  }
  
  int main() {
  
     B b;
  
     f(dynamic_cast&lt;const A &amp;&gt;(b));
  
  }
  </code></pre>
  
  <p>Note that if I take out the <code>dynamic_cast</code> the code will work yet the <em>second</em> <code>f</code> is called (it prints "Generic"). But what I'm trying to do is to get the first call. I figured a <code>dynamic_cast</code> would work, but for some reason it causes problems. What am I doing wrong here?</p>