title: Overloading a C++ template class virtual function
body: <p>Below is the code to recreate the problem I am having. <code>Base</code> class is a template class with the virtual function <code>foo</code>. <code>foo</code> has a default implementation that adds the passed in arguments.</p>
  
  <p><code>SimpleDerived</code> derives from <code>Base</code>, specializing it with <code>std::string</code>. <code>SimpleDerived</code> overloads the virtual <code>Base&lt;T&gt;::foo()</code> function. This class compiles fine and its <code>foo</code> outputs as expected when called in <code>main</code>.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;class T&gt;
  struct Base
  {
      virtual void foo(T val)
      {
          T local = val + val;    // THE OFFENDING LINE OF CODE
          std::cout &lt;&lt; "Base" &lt;&lt; std::endl;
      }   
  };
  
  struct SimpleDerived : public Base&lt;std::string&gt;
  {
      virtual void foo(std::string val)
      {
          std::cout &lt;&lt; "SimpleDerived" &lt;&lt; std::endl;
      }
  };
  
  struct SimpleObject
  {
      int value;
  };
  
  struct ComplexDerived : public Base&lt;SimpleObject&gt;
  {
      virtual void foo(SimpleObject val)
      {
          std::cout &lt;&lt; "ComplexDerived" &lt;&lt; std::endl;
      }   
  };
  
  int main(void)
  {
      Base&lt;int&gt; base;
      base.foo(2);
  
      SimpleDerived simpleDerived;
      simpleDerived.foo("hello world");
  
      SimpleObject object;
      ComplexDerived complexDerived;
      complexDerived.foo(object);
  
      return 0;
  }
  </code></pre>
  
  <p><code>ComplexDerived</code> derives from <code>Base</code>, specializing it with a custom struct <code>SimpleObject</code>. <code>ComplexDerived</code> overloads <code>foo</code> as well. However, this is the root of the problem. If I try to compile this I get:</p>
  
  <pre><code>quicktest.cpp: In member function ‘void Base&lt;T&gt;::foo(T) [with T = SimpleObject]’:
  quicktest.cpp:47:1:   instantiated from here
  quicktest.cpp:8:19: error: no match for ‘operator+’ in ‘val + val’
  </code></pre>
  
  <p>Obviously, there is no operator "+" for <code>SimpleObject</code>. But here is my confusion.. the compiler is being asked to implement <code>Base&lt;SimpleObject&gt;::foo</code> because this is what <code>ComplexDerived</code> inherits from. However, I never use or call <code>Base&lt;SimpleObject&gt;::foo</code>. So should the compiler be trying to generate this base class function?</p>