title: How to specialize std::hash<Key>::operator() for user-defined type in unordered containers?
body: <p>To support user-defined key types in <code>std::unordered_set&lt;Key&gt;</code> and <code>std::unordered_map&lt;Key, Value&gt;</code>
  one has to provide <code>operator==(Key, Key)</code> and a hash functor:</p>
  
  <pre><code>struct X { int id; /* ... */ };
  bool operator==(X a, X b) { return a.id == b.id; }
  
  struct MyHash {
    size_t operator()(const X&amp; x) const { return std::hash&lt;int&gt;()(x.id); }
  };
  
  std::unordered_set&lt;X, MyHash&gt; s;
  </code></pre>
  
  <p>It would be more convenient to write just <code>std::unordered_set&lt;X&gt;</code>
  with a <em>default hash</em> for type <code>X</code>, 
  like for types coming along with the compiler and library.
  After consulting </p>
  
  <ul>
  <li>C++ Standard <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf" rel="noreferrer">Draft N3242</a> §20.8.12 [unord.hash] and §17.6.3.4 [hash.requirements],  </li>
  <li><a href="http://www.boost.org/doc/libs/1_48_0/doc/html/unordered.html" rel="noreferrer">Boost.Unordered</a></li>
  <li>g++ <code>include\c++\4.7.0\bits\functional_hash.h</code> </li>
  <li>VC10 <code>include\xfunctional</code></li>
  <li>various <a href="https://stackoverflow.com/questions/647967/how-to-extend-stdtr1hash-for-custom-types">related question</a>s in Stack Overflow</li>
  </ul>
  
  <p>it seems possible to specialize <code>std::hash&lt;X&gt;::operator()</code>:</p>
  
  <pre><code>namespace std { // argh!
    template &lt;&gt;
    inline size_t 
    hash&lt;X&gt;::operator()(const X&amp; x) const { return hash&lt;int&gt;()(x.id); } // works for MS VC10, but not for g++
    // or
    // hash&lt;X&gt;::operator()(X x) const { return hash&lt;int&gt;()(x.id); }     // works for g++ 4.7, but not for VC10 
  }                                                                             
  </code></pre>
  
  <p>Given compiler support for C++11 is yet experimental---I did not try Clang---, these are my questions:      </p>
  
  <ol>
  <li><p>Is it legal to add such a specialization to namespace <code>std</code>? I have mixed feelings about that. </p></li>
  <li><p>Which of the <code>std::hash&lt;X&gt;::operator()</code> versions, if any, is compliant with C++11 standard?</p></li>
  <li><p>Is there a portable way to do it?</p></li>
  </ol>