title: C++ template specialization for map-like types
body: <p>I want to define a generic function for printing contents of <code>std::map</code> like types. My initial attempt is a function like this:</p>
  
  <pre><code>template &lt;class K, class V&gt;
  inline void PrintCollection(const std::map&lt;K,V&gt;&amp; map,
                              const char* separator="\n",
                              const char* arrow="-&gt;",
                              const char* optcstr="") {
    typedef typename std::map&lt;K,V&gt;::const_iterator iter_type;
    std::cout &lt;&lt; optcstr;
    for (iter_type begin = map.begin(), it = begin, end = map.end();
         it != end; ++it) {
      if (it != begin) {
        std::cout &lt;&lt; separator;
      }
      std::cout &lt;&lt; it-&gt;first &lt;&lt; arrow &lt;&lt; it-&gt;second;
    }
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>which works fine. When I try to generalize this function one more step, i.e. make it work for <code>std::multimap</code> type, compiler becomes angry. I tried several ways to make <code>std::map</code> generic in the function definition, such as:</p>
  
  <pre><code>template &lt;class M, class K, class V&gt;
  inline void PrintCollection(const M&lt;K,V&gt;&amp; map,
                              const char* separator="\n",
                              const char* arrow="-&gt;",
                              const char* optcstr="") {
    typedef typename M&lt;K,V&gt;::const_iterator iter_type;
    std::cout &lt;&lt; optcstr;
    for (iter_type begin = map.begin(), it = begin, end = map.end();
         it != end; ++it) {
      if (it != begin) {
        std::cout &lt;&lt; separator;
      }
      std::cout &lt;&lt; it-&gt;first &lt;&lt; arrow &lt;&lt; it-&gt;second;
    }
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>with no success.</p>
  
  <p>How can I generalize this function as I defined above?</p>
  
  <p>To be more clear, I have already a function defined for vector-like classes defined before this function. It is like</p>
  
  <pre><code>template &lt;class T&gt;
  inline void PrintCollection(const T&amp; collection,
                              const char* separator="\n",
                              const char* optcstr="") {
    typedef typename T::const_iterator iter_type;
  
    std::cout &lt;&lt; optcstr;
  
    for (iter_type begin = collection.begin(), it = begin, end = collection.end();
         it != end;
         ++it) {
      if (it != begin) {
        std::cout &lt;&lt; separator;
      }
      std::cout &lt;&lt; *it;
    }
  
    std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>So what I want to achieve it to make this function specialized to map-like classes. I'm pretty new in C++, so I don't know the exact term for this kind of stuff. Is this called "template specialization"?</p>