title: Automatically deducing element type of pair container from the function template argument
body: <p>I want to write a template function which takes a container of key-value pairs (e.g <code>map&lt;K,V&gt;</code> or <code>vector&lt;pair&lt;K,V&gt;&gt;</code>) and returns a container of container of keys. For example:</p>
  
  <pre><code>template&lt;typename C, typename K, typename V&gt;
  vector&lt;vector&lt;K&gt;&gt; partition_keys(const C&amp; input)
  {
      vector&lt;vector&lt;K&gt;&gt; result;
      ...
      // do something
      for (const auto &amp;pair : input)
      {
          cout &lt;&lt; pair.first &lt;&lt; "," &lt;&lt; pair.second &lt;&lt; std::endl;
      }
      ...
      return result;
  }
  </code></pre>
  
  <p>Here's how I would like to call it:</p>
  
  <pre><code>// map
  map&lt;string, string&gt; product_categories;
  partition_keys(product_categories); // doesn't work
  partition_keys&lt;map&lt;string, string&gt;, string, string&gt;(product_categories); // have to do this
  // vector
  vector&lt;pair&lt;string, string&gt;&gt; movie_genres;
  partition_keys(movie_genres); // doesn't work
  partition_keys&lt;vector&lt;pair&lt;string, string&gt;&gt;, string, string&gt;(movie_genres); // have to do this
  </code></pre>
  
  <p>But, the compiler cannot deduce the template parameters K and V without explicitly specifying them. I want the function to work with any container having pair of any type; so I want to avoid writing separate template functions for <code>map&lt;K,V&gt;</code>, <code>list&lt;pair&lt;K,V&gt;&gt;</code>, <code>vector&lt;pair&lt;K,V&gt;&gt;</code>, etc.</p>
  
  <p>So, I had to modify the template function signature as follows to make it work the way I want:</p>
  
  <pre><code>template&lt;typename C, 
           typename K = remove_const_t&lt;C::value_type::first_type&gt;, 
           typename V = C::value_type::second_type&gt;
  vector&lt;vector&lt;K&gt;&gt; partition_keys(const C&amp; input);
  </code></pre>
  
  <p>Is there a better way to do this? Is it a good practice to deduce types of <code>K</code> and <code>V</code> based on <code>value_type</code> of <code>C</code>? Also, there is a possibility of caller explicitly passing invalid arguments for <code>K</code> and <code>V</code>.</p>
  
  <p>Note also how I had remove constness of key type by calling <code>remove_const_t</code> because for a <code>map</code>, <code>C::value_type::first_type</code> is a <code>const</code> type and the standard doesn't allow creating a collection of a <code>const</code> type.</p>