title: C++ maps, vectors, pairs with references
body: <p>I am trying to share references between a map and another vector as in the following code:
  </p>
  
  <pre><code>template &lt;typename T&gt;
  void f(T&amp; x) {
  
    std::map&lt;T, vector&lt;int&gt; &gt; mp;
    mp[x] = vector&lt;int&gt;(2, 4);
  
    vector&lt; pair&lt;T&amp;, int&gt; &gt; v1;
  
    for (auto&amp; kv : mp) {
      v1.push_back( make_pair(kv.first, 0) );
    }
  
  }
  </code></pre>
  
  <p>and call this function from another
  </p>
  
  <pre><code>int x = 2;
  f(x);
  </code></pre>
  
  <p>But this does not compile.</p>
  
  <pre><code>error: no matching function for call to ‘std::vector&lt;std::pair&lt;int&amp;, int&gt;, std::allocator&lt;std::pair&lt;int&amp;, int&gt; &gt; &gt;::push_back(std::pair&lt;int, int&gt;)’
       v1.push_back( make_pair(kv.first, 0) );
  </code></pre>
  
  <p>Does anybody see why?</p>
  
  <p><strong>EDIT</strong>
  I was able to make things work with</p>
  
  <pre><code>std::reference_wrapper
  </code></pre>
  
  <p>Here is my current solution.</p>
  
  <pre><code>template &lt;typename T&gt;
  void f(T&amp; x) {
  
    std::map&lt;std::reference_wrapper&lt;T&gt;, vector&lt;int&gt; &gt; mp;
    mp[x] = vector&lt;int&gt;(2, 4);
  
    vector&lt; pair&lt;T&amp;, int&gt; &gt; v1;
  
    for (auto&amp; kv : mp) {
      v1.push_back( make_pair( std::ref(kv.first) , 0) );
    }
  
    v1[0].first = 34;
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>int x = 2;
  f(x);
  cout &lt;&lt; x &lt;&lt; endl;
  </code></pre>
  
  <p>prints 34. Do people see any cons with this vs the pointer based approach?</p>