title: Function name resolution in base class
body: <p>Consider the following code snippet,</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;memory&gt;
  
  template &lt;typename Derived&gt;
  class Base
  {
  public:
      int foo()
      {
          return static_cast&lt;Derived*&gt;(this)-&gt;bar(); // I expected a compilation error here
      }
  
  private:
      int bar()
      {
          return 1;
      }
  };
  
  class Derived : public Base&lt;Derived&gt; {};
  
  int baz()
  {
      return std::unique_ptr&lt;Derived&gt;()-&gt;foo();
  }
  </code></pre>
  
  <p>From my understanding, the template class <code>Base</code> declares the function <code>bar</code> as private and is hence not visible to even <code>Derived</code> class, let alone the users of any <code>Derived</code> object.</p>
  
  <p>In the public function <code>foo</code> of the template class Base, I try to access a non-existing function <code>bar</code> through a <code>Derived</code> object. I expected the compiler to fail to find the function <code>bar</code>.</p>
  
  <p><strong>Q. How does the compiler find the private <code>bar</code> function of the <code>Base</code> template class through the use of a <code>Derived</code> object?</strong> <a href="https://godbolt.org/z/jVxW-k" rel="nofollow noreferrer">https://godbolt.org/z/jVxW-k</a></p>
  
  <p>Now consider a slight variation to the snippet in which the <code>Derived</code> class is no longer empty.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Derived : public Base&lt;Derived&gt;
  {
  private:
      int bar()
      {
          return 1;
      }
  };
  </code></pre>
  
  <p>Here, the output is expected with the compiler erroring out with the message,</p>
  
  <pre><code>&lt;source&gt;: In instantiation of 'int Base&lt;Derived&gt;::foo() [with Derived = Derived]':
  
  &lt;source&gt;:30:44:   required from here
  
  &lt;source&gt;:9:49: error: 'int Derived::bar()' is private within this context
  
      9 |         return static_cast&lt;Derived*&gt;(this)-&gt;bar();
  
        |                                                 ^
  
  &lt;source&gt;:22:9: note: declared private here
  
     22 |     int bar()
  
        |         ^~~
  
  Compiler returned: 1
  </code></pre>
  
  <p><strong>Q. Why wasn't the compiler able to find the private function <code>bar</code> now? Is there some way in which I can force Derived implementations to implement the function <code>bar</code>?</strong> <a href="https://godbolt.org/z/PrkNo6" rel="nofollow noreferrer">https://godbolt.org/z/PrkNo6</a></p>
  
  <p>Compiler used: g++ 9.2.
  Compiler flags used: <code>-std=c++17 -O3 -Wall -Werror</code>.</p>
  
  <p>Thanks in Advance!!!</p>