title: c++ overloading operator[] for std::pair
body: <p>I work a lot with pairs of values: <code>std::pair&lt;int, int&gt;   my_pair</code>.  Sometimes I need to perform the same operation on both <code>my_pair.first</code> and <code>my_pair.second</code>.</p>
  
  <p>My code would be much smoother if I could do <code>my_pair[j]</code> and loop over  <em>j=0,1</em>.
  (I am avoiding using arrays because I don't want to bother with allocating memory, and I use <code>pair</code> extensively with other things).</p>
  
  <p>Thus, I would like to <em>define</em> <code>operator[]</code> for <code>std::pair&lt;int, int&gt;</code>.</p>
  
  <p>And I can't get it to work, (I'm not very good with templates and such)...</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;stdlib.h&gt;
  
  template &lt;class T1&gt; T1&amp; std::pair&lt;T1, T1&gt;::operator[](const uint &amp;indx) const
  {
    if (indx == 0)
      return first;
    else
      return second;
  };
  
  int main()
  {
  // ....
  return 0;
  }
  </code></pre>
  
  <p>fails to compile.  Other variations fail as well.</p>
  
  <p>As far as I can tell, I am following the <a href="https://stackoverflow.com/questions/4421706/operator-overloading/4421729">Stack Overflow operator overloading FAQ</a>, but I guess I am missing something...</p>