title: Using std::reference_wrapper as the key in a std::map
body: <p>I have a bunch of objects in a class hierarchy and would like to make a <code>std::map</code> using references to those objects as the keys in the map.  Its seems like <code>std::reference_wrapper</code> would be exactly what is needed for this, but I can't seem to make it work.  What I've tried so far:</p>
  
  <pre><code>class Object { // base class of my hierarchy
      // most details unimportant
  public
      virtual bool operator&lt; (const Object &amp;) const;  // comparison operator
  };
  
  std::map&lt;std::reference_wrapper&lt;const Object&gt;, int&gt; table;
  
  auto it = table.find(object);
  
  table[object] = 42;
  
  table[object]++
  </code></pre>
  
  <p>However, I always get somewhat obscure errors from the compiler:</p>
  
  <pre><code>/usr/include/c++/4.5.3/bits/stl_function.h: In member function ‘bool std::less&lt;_Tp&gt;::operator()(const _Tp&amp;, const _Tp&amp;) const [with _Tp = std::reference_wrapper&lt;const Object&gt;]’:
  /usr/include/c++/4.5.3/bits/stl_tree.h:1522:38:   instantiated from ‘std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::iterator std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::find(const _Key&amp;) [with _Key = std::reference_wrapper&lt;const Object&gt;, _Val = std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt;, _KeyOfValue = std::_Select1st&lt;std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt; &gt;, _Compare = std::less&lt;std::reference_wrapper&lt;const Object&gt; &gt;, _Alloc = std::allocator&lt;std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt; &gt;, std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::iterator = std::_Rb_tree_iterator&lt;std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt; &gt;]’
  /usr/include/c++/4.5.3/bits/stl_map.h:697:29:   instantiated from ‘std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::iterator std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::find(const key_type&amp;)[with _Key = std::reference_wrapper&lt;const Object&gt;, _Tp = int, _Compare = std::less&lt;std::reference_wrapper&lt;const Object&gt; &gt;, _Alloc = std::allocator&lt;std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt; &gt;, std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::iterator = std::_Rb_tree_iterator&lt;std::pair&lt;const std::reference_wrapper&lt;const Object&gt;, int&gt; &gt;, key_type = std::reference_wrapper&lt;const Object&gt;]’
  testfile.cpp:39:31:   instantiated from here
  /include/c++/4.5.3/bits/stl_function.h:230:22: error: no match for ‘operator&lt;’ in ‘__x &lt; __y’
  </code></pre>
  
  <p>The error seems to be saying it can't compare two <code>std::reference_wrapper&lt;const Object&gt;</code> objects, but it seems like it should be possible -- <code>std::reference_wrapper</code> has a conversion operator that can implicitly convert it to a <code>T&amp;</code> (<code>const Object &amp;</code> here), and <code>Object</code> has a <code>operator &lt;</code>, so why doesn't it work?</p>
  
  <p>Should it work and this is merely a bug in g++?  Or is something else going on?</p>