title: Invoke std::hash() through constructor in unordered_map wrapper in C++
body: <p>We are trying to set <code>mHashCode</code> either through template specialization of <code>operator()</code>(string in this case) or through the c'tor of <code>HashFunction()</code> by the user provided key(here, <code>inKey</code>).</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  using namespace std;
  
  template&lt; class Key &gt; class HashFunction
  {
    public:
      HashFunction( const Key &amp; k );
  
      inline operator unsigned() const
      {
        return mHashCode;
      }
      size_t operator()(const Key &amp;k) const;
  
    private:
      unsigned mHashCode;
  };
  
  template&lt;&gt; HashFunction&lt; string &gt;::HashFunction( const string &amp; k )
  {
    mHashCode=std::hash&lt;string&gt;{}(k);
  }
  
  template &lt;&gt; size_t HashFunction&lt; string &gt;::operator()(const string &amp;k) const 
  {
    return mHashCode;
  }
  
  template&lt; class Key, class Val, class Hash = HashFunction&lt; Key &gt; &gt;
  class unordered_map_wrapper
  {
    public:
     unordered_map_wrapper();
    private:
      unordered_map&lt;Key, Val, Hash&gt; * mTable;
  };
  
  template&lt; class Key, class Val, class Hash &gt;
  unordered_map_wrapper&lt; Key, Val, Hash &gt;::unordered_map_wrapper()
  {
      mTable=new unordered_map&lt;Key, Val, Hash&gt;(5);
  }
  
  int main() {
      unordered_map_wrapper&lt;string, unsigned&gt; h;
      return 0;
  }
  </code></pre>
  
  <p>If the user is passing the hash function(not match with <code>string</code>, <code>unsigned</code> etc.) along with the <code>hash()</code> constructor, this <code>mHashCode</code> is generated by hitting the c'tor of <code>HashFunction()</code>. An object of <code>HashFunction</code> class will be created in this case. Due to this reason, <code>mHashCode</code> will generate and default template specialization of <code>operator()</code> will get control and return <code>mHashCode</code>. i.e. it's returning user's value only.</p>
  
  <p>Template specialization is using <code>operator unsigned()</code>(specilization of constructor), creating an object and returning <code>mHashCode</code>.</p>
  
  <p>Its working fine if we are passing hash function with the help of template argument and by default, it's using operator(), not the c'tor of <code>HashFunction()</code>. Getting a compile time error if we are using c'tor like the above snippet.</p>
  
  <p>How could we set <code>mHashCode</code> through this c'tor so that user can use <code>std::Hash()</code>? Our hard requirement is that we need to invoke <code>std::hash()</code> through c'tor, not through the operator.</p>
  
  <p>Thanks in advance! </p>