title: Moving keys from unordered_map
body: <p>I've searched but I only found questions about move constructor with the <em>mapped value</em>, but I want to try something different.</p>
  
  <p>Is it possible to use <code>std::move</code> the <strong>key</strong> from a <code>std::unordered_map</code>? The reason is quite simple: I'd like to construct an example where I create a vector from the map, wasting as little as possible of memory. I know it would mess up with the representation of the map, but hey, after all I will never use the map again, so it would make sense to move values out.</p>
  
  <p>My guess is this: no, I cannot do that. However, I'd like some confirmation. </p>
  
  <p>Here's a simple code. I expected to see the move constructor called, but I have the copy constructor  called.</p>
  
  <p>Cheers &amp; Thanks!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;utility&gt;
  
  class prop
  {
  public:
      prop(const std::string &amp;s, int i) : s_(s), i_(i) { std::cout &lt;&lt; "COPIED" &lt;&lt; std::endl; };
  
      prop(std::string &amp;&amp;s, int i) : s_(std::move(s)), i_(i) { std::cout &lt;&lt; "MOVED" &lt;&lt; std::endl; };
  
      std::string s_;
      int         i_;
  };
  
  std::string gen_random(const int len) {
      static const char alphanum[] =
      "ABC";
  
      std::string s;
      s.resize(len);
  
      for (int i = 0; i &lt; len; ++i) {
          s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
      }
  
      return s;
  }
  
  int main()
  {
      const long n = 3, len = 4, max = 20;
  
      std::unordered_map&lt;std::string, int&gt; map;
  
      std::cout &lt;&lt; "&gt;&gt;GENERATING" &lt;&lt; std::endl;
      for (int i = 0; i &lt; n; i++) map[gen_random(len)]++;
  
      if (map.size() &lt; max)
      {
          std::cout &lt;&lt; "&gt;&gt;MAP" &lt;&lt; std::endl;
          for (auto &amp;p : map) std::cout &lt;&lt; p.first &lt;&lt; " : " &lt;&lt; p.second &lt;&lt; std::endl;
      }
  
      std::cout &lt;&lt; "&gt;&gt;POPULATING VEC" &lt;&lt; std::endl;
      std::vector&lt;prop&gt; vec;
      vec.reserve(map.size());
      for (auto &amp;p : map) vec.push_back(prop(p.first, p.second));
  
      if (map.size() &lt; max)
      {
          std::cout &lt;&lt; "&gt;&gt;VEC" &lt;&lt; std::endl;
          for (auto &amp;p : vec) std::cout &lt;&lt; p.s_ &lt;&lt; " : " &lt;&lt; p.i_ &lt;&lt; std::endl;
          std::cout &lt;&lt; "&gt;&gt;MAP" &lt;&lt; std::endl;
          for (auto &amp;p : map) std::cout &lt;&lt; p.first &lt;&lt; " : " &lt;&lt; p.second &lt;&lt; std::endl;
      }
  
      std::cout &lt;&lt; "&gt;&gt;POPULATING MOV" &lt;&lt; std::endl;
      std::vector&lt;prop&gt; mov;
      mov.reserve(map.size());
      for (auto &amp;p : map) mov.push_back(prop(std::move(p.first), p.second));
  
      if (map.size() &lt; max)
      {
          std::cout &lt;&lt; "&gt;&gt;MOV" &lt;&lt; std::endl;
          for (auto &amp;p : mov) std::cout &lt;&lt; p.s_ &lt;&lt; " : " &lt;&lt; p.i_ &lt;&lt; std::endl;
          std::cout &lt;&lt; "&gt;&gt;MAP" &lt;&lt; std::endl;
          for (auto &amp;p : map) std::cout &lt;&lt; p.first &lt;&lt; " : " &lt;&lt; p.second &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p><strong>output</strong></p>
  
  <pre><code>&gt;&gt;GENERATING
  &gt;&gt;MAP
  CBAC : 1
  BCAC : 1
  BBCC : 1
  &gt;&gt;POPULATING VEC
  COPIED
  COPIED
  COPIED
  &gt;&gt;VEC
  CBAC : 1
  BCAC : 1
  BBCC : 1
  &gt;&gt;MAP
  CBAC : 1
  BCAC : 1
  BBCC : 1
  &gt;&gt;POPULATING MOV
  COPIED
  COPIED
  COPIED
  &gt;&gt;MOV
  CBAC : 1
  BCAC : 1
  BBCC : 1
  &gt;&gt;MAP
  CBAC : 1
  BCAC : 1
  BBCC : 1
  Program ended with exit code: 0
  </code></pre>