title: equal_range and range for
body: <p>While discussing <code>multimap</code> with my students, I noticed a small change that could cut out a bit of boilerplate, and was wondering if anyone had suggested it to the standard committee, and if so what the response was.</p>
  
  <p>The canonical method of iterating over an equal range is (taken from cplusplus.com):</p>
  
  <pre><code>// multimap::equal_range
  #include &lt;iostream&gt;
  #include &lt;map&gt;
  
  int main ()
  {
    std::multimap&lt;char,int&gt; mymm;
  
    mymm.insert(std::pair&lt;char,int&gt;('a',10));
    mymm.insert(std::pair&lt;char,int&gt;('b',20));
    mymm.insert(std::pair&lt;char,int&gt;('b',30));
    mymm.insert(std::pair&lt;char,int&gt;('b',40));
    mymm.insert(std::pair&lt;char,int&gt;('c',50));
    mymm.insert(std::pair&lt;char,int&gt;('c',60));
    mymm.insert(std::pair&lt;char,int&gt;('d',60));
  
    std::cout &lt;&lt; "mymm contains:\n";
    for (char ch='a'; ch&lt;='d'; ch++)
    {
      std::pair &lt;std::multimap&lt;char,int&gt;::iterator,std::multimap&lt;char,int&gt;::iterator&gt; ret;
      ret = mymm.equal_range(ch);
      std::cout &lt;&lt; ch &lt;&lt; " =&gt;";
      for (std::multimap&lt;char,int&gt;::iterator it=ret.first; it!=ret.second; ++it)
        std::cout &lt;&lt; ' ' &lt;&lt; it-&gt;second;
      std::cout &lt;&lt; '\n';
    }
  
    return 0;
  }
  </code></pre>
  
  <p>You cannot use a range based for loop directly in this case because the return type of equal_range is a <code>pair&lt;multimap&lt;K,V&gt;::iterator, multimap&lt;K,V&gt;::iterator&gt;</code>.  However, a simple wrapping struct should allow this:</p>
  
  <pre><code>template &lt;typename T&gt;
  struct abstract_collection {
    abstract_collection(pair&lt;T, T&gt; its)
    : m_begin(its.first),
      m_end(its.second) {}
    abstract_collection(T begin, T end)
    : m_begin(begin),
      m_end(end) {}
  
    T begin() const { return m_begin; }
    T end() const { return m_end; }
  
    T m_begin;
    T m_end;
  };
  </code></pre>
  
  <p>Combined with adding a function to the multimap (and others) API to return iterators in this structure, rather than in a pair.</p>
  
  <pre><code>template&lt;typename K, typename V, typename C, typename A&gt;
  auto multimap&lt;K, V, C, A&gt;::equal_range_c(K const&amp; k) -&gt; abstract_collection&lt;iterator&gt; {
    return equal_range(k);
  }
  </code></pre>
  
  <p>Or alternatively overloading a version of <code>std::begin</code> and <code>std::end</code> that takes a pair of iterators should work as well:</p>
  
  <pre><code>template &lt;typename T&gt;
  T begin(pair&lt;T, T&gt; p) { return p.first; }
  
  template &lt;typename T&gt;
  T end(pair&lt;T, T&gt; p) { return p.second; }
  </code></pre>
  
  <p>Has these ideas surfaced before, and if so, what was the committee response?  Are they just unworkable or undesirable for some reason I'm not seeing?</p>
  
  <p>(Note, the code was written without attempting to compile or check for expositional purposes only.  It's probably wrong.  And it doesn't contain typechecking to constrain to iterators only as it ought to, as that was added complexity that didn't serve to explain the idea.)</p>