title: Priority when choosing overloaded template functions in C++
body: <p>I have the following problem:</p>
  
  <pre><code>class Base
  {
  };
  
  class Derived : public Base
  {
  };
  
  class Different
  {
  };
  
  class X
  {
  public:
    template &lt;typename T&gt;
    static const char *func(T *data)
    {
      // Do something generic...
      return "Generic";
    }
  
    static const char *func(Base *data)
    {
      // Do something specific...
      return "Specific";
    }
  };
  </code></pre>
  
  <p>If I now do</p>
  
  <pre><code>Derived derived;
  Different different;
  std::cout &lt;&lt; "Derived: " &lt;&lt; X::func(&amp;derived) &lt;&lt; std::endl;
  std::cout &lt;&lt; "Different: " &lt;&lt; X::func(&amp;different) &lt;&lt; std::endl;
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>Derived: Generic
  Different: Generic
  </code></pre>
  
  <p>But what I want is that for all classes derived from Base the specific method is called.
  So the result should be:</p>
  
  <pre><code>Derived: Specific
  Different: Generic
  </code></pre>
  
  <p>Is there any way I can redesign the X:func(...)s to reach this goal?</p>
  
  <p><b>EDIT:</b></p>
  
  <p>Assume that it is not known by the caller of X::func(...) if the class submitted as the parameter is derived from Base or not. So Casting to Base is not an option.
  In fact the idea behind the whole thing is that X::func(...) should 'detect' if the parameter is derived from Base or not and call different code.
  And for performance reasons the 'detection' should be made at compile time.</p>