title: Insert elements into std::map without extra copying
body: <p>Consider this program:</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;string&gt;
  #define log magic_log_function // Please don't mind this.
  
  //
  // ADVENTURES OF PROGO THE C++ PROGRAM
  //
  
  class element;
  typedef std::map&lt;int, element&gt; map_t;
  
  class element {
  public:
      element(const std::string&amp;);
      element(const element&amp;);
      ~element();
      std::string name;
  };
  element::element(const std::string&amp; arg)
      : name(arg)
  {
      log("element ", arg, " constucted, ", this);
  }
  element::element(const element&amp; other)
      : name(other.name)
  {
      name += "-copy";
      log("element ", name, " copied, ", this);
  }
  element::~element()
  {
      log("element ", name, " destructed, ", this);
  }
  int main(int argc, char **argv)
  {
      map_t map1; element b1("b1");
      log(" &gt; Done construction.");
      log(" &gt; Making map 1.");
      map1.insert(std::pair&lt;int, element&gt;(1, b1));
      log(" &gt; Done making map 1.");
      log(" &gt; Before returning from main()");
  }
  </code></pre>
  
  <p>It creates some objects on stack and <code>insert</code>s them into an <code>std::map</code> container, creating <strong>two</strong> extra temporary copies in the process:</p>
  
  <pre><code>element b1 constucted, 0x7fff228c6c60
   &gt; Done construction.
   &gt; Making map 1.
  element b1-copy copied, 0x7fff228c6ca8
  element b1-copy-copy copied, 0x7fff228c6c98
  element b1-copy-copy-copy copied, 0x232d0c8
  element b1-copy-copy destructed, 0x7fff228c6c98
  element b1-copy destructed, 0x7fff228c6ca8
   &gt; Done making map 1.
   &gt; Before returning from main()
  element b1 destructed, 0x7fff228c6c60
  element b1-copy-copy-copy destructed, 0x232d0c8
  </code></pre>
  
  <p>We can get rid of one extra copy constructor call by changing the <code>std::pair</code> signature to <code>std::pair&lt;int, element&amp;&gt;</code>, however, the second temporary is still created and immediately destroyed:</p>
  
  <pre><code>element b1 constucted, 0x7fff0fe75390
   &gt; Done construction.
   &gt; Making map 1.
  element b1-copy copied, 0x7fff0fe753c8
  element b1-copy-copy copied, 0x1bc4098
  element b1-copy destructed, 0x7fff0fe753c8
   &gt; Done making map 1.
   &gt; Before returning from main()
  element b1 destructed, 0x7fff0fe75390
  element b1-copy-copy destructed, 0x1bc4098
  </code></pre>
  
  <p>Is there a way to make <code>std::map</code> just take an object on stack by reference and make a single internal copy of it?</p>