title: C++ Template Specialization / Partial and Full
body: <p>I was trying to understand Template Partial and Full Specialization and have this below code:</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  //Template in C++is a feature. We write code once and use it for any data type including user defined data types.
  //What if we want a different code for a particular data type - Template Specialization
  template &lt;class T, class Y&gt;
  void fun(T a, Y b)
  {
      cout &lt;&lt; &quot;The main template fun(): &quot; &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt;  b &lt;&lt; endl;
  }
  //If a specialized version is present, compiler first checks with the specialized version and then the main template.
  //Compiler first checks with the most specialized version by matching the passed parameter with the data type(s) specified in a specialized version.
  template&lt;&gt;
  void fun(const char* a, const char* b)
  {
      cout &lt;&lt; &quot;Specialized Template for int type: &quot; &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt;  b &lt;&lt; endl;
  }
  //Partial specialization allows to specialize only some arguments of a class template, as opposed to explicit full
  template&lt;class T&gt;
  void fun(T a, int b)
  {
      cout &lt;&lt; &quot;Partial Template specialization for int type: &quot; &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt; b &lt;&lt; endl;
  }
  int main()
  {
      fun&lt;int, int&gt;('a', 10);
      fun&lt; const char*, const char*&gt;(&quot;Hello&quot;, &quot;Morning&quot;);
      fun&lt;float, float&gt;(10.14, 19.89);
  }
  </code></pre>
  <p>Note that in main I am specifying the data types and below is the output:</p>
  <pre><code>The main template fun(): 97 10
  Specialized Template for int type: Hello Morning
  The main template fun(): 10.14 19.89  
  </code></pre>
  <p>But when I execute the main code below way:</p>
  <pre><code>int main()
  {
      fun('a', 10);
      fun(&quot;Hello&quot;, &quot;Morning&quot;);
      fun(10.14, 19.89);
  }
  </code></pre>
  <p>This is the output I get:</p>
  <pre><code>Partial Template specialization for int type: a 10
  Specialized Template for int type: Hello Morning
  The main template fun(): 10.14 19.89 
  </code></pre>
  <p>So what does the actual C++ Template Partial / Full specialization states - do we need to specify the data types in template argument to invoke - also in many websites I have seen following signature for Partial specialization:</p>
  <pre><code>template&lt;class Y, const char*&gt;
  void fun(Y a, const char* b)
  </code></pre>
  <p>Rather than</p>
  <pre><code>template&lt;class Y&gt;
  void fun(Y a, const char* b)
  </code></pre>
  <p>Similarly for full specialization - what is the exact way to write and call Partial / Full template specialized function / class?</p>