title: unordered_map excess calls to hash function
body: <p>The following code results with unexplained calls to the hash function:</p>
  <pre><code>namespace foo {
      using Position = tuple &lt;int, int, int&gt;;
      
      std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const Position&amp; pos) noexcept{
          return out &lt;&lt; get&lt;0&gt;(pos) &lt;&lt; &quot;, &quot; &lt;&lt; get&lt;1&gt;(pos) &lt;&lt; &quot;, &quot; &lt;&lt; get&lt;2&gt;(pos);
      }
  
      struct hashFunc{
          std::size_t operator()(const Position&amp; pos) const noexcept{
              int res = get&lt;0&gt;(pos) * 17 ^ get&lt;1&gt;(pos) * 11 ^ get&lt;2&gt;(pos);
              cout &lt;&lt; &quot;@@@ hash function called for key: &quot; &lt;&lt; pos 
                   &lt;&lt; &quot;, hash: &quot; &lt;&lt; res &lt;&lt; endl;
              return res;
          }
      };
  
      template&lt;typename T&gt;
      void print_buckets(T&amp;&amp; map) {
          auto num_buckets = map.bucket_count();
          cout &lt;&lt; &quot;------------------------------&quot; &lt;&lt; endl;
          cout &lt;&lt; &quot;NUM BUCKETS: &quot; &lt;&lt; num_buckets &lt;&lt; endl;
          for(size_t i=0; i&lt;num_buckets; ++i) {
              auto bucket_size = map.bucket_size(i);
              if(bucket_size) {
                  cout &lt;&lt; &quot;BUCKET &quot; &lt;&lt; i &lt;&lt; &quot; size: &quot; &lt;&lt; bucket_size &lt;&lt; endl;        
              }
          }
          cout &lt;&lt; &quot;------------------------------&quot; &lt;&lt; endl;
      }
  }
  </code></pre>
  <p>Main:</p>
  <pre><code>using namespace foo;
  
  int main() {
      // note: bucket_count specified
      unordered_map &lt;Position, std::string, hashFunc&gt; test(10); 
      
      auto x = tuple{1,0,0};
      auto z = tuple{0,1,0};
      auto w = tuple{0,0,1};
              
      cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;about to insert: &quot; &lt;&lt; x &lt;&lt; endl;
      test[x] =  &quot;hello&quot;;
      print_buckets(test);
      cout &lt;&lt; &quot;after insert of: &quot; &lt;&lt; x &lt;&lt; endl;
      
      cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;about to insert: &quot; &lt;&lt; z &lt;&lt; endl;
      test[z] = &quot;hey&quot;;
      print_buckets(test);
      cout &lt;&lt; &quot;after insert of: &quot; &lt;&lt; z &lt;&lt; endl;
      
      cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;about to insert: &quot; &lt;&lt; w &lt;&lt; endl;
      test.insert({w, &quot;hello&quot;});
      print_buckets(test);
      cout &lt;&lt; &quot;after insert of: &quot; &lt;&lt; w &lt;&lt; endl;    
      cout &lt;&lt; &quot;==================================&quot; &lt;&lt; endl;
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>==================================
  about to insert: 1, 0, 0
  @@@ hash function called for key: 1, 0, 0, hash: 17
  ------------------------------
  NUM BUCKETS: 11
  BUCKET 6 size: 1
  ------------------------------
  after insert of: 1, 0, 0
  ==================================
  about to insert: 0, 1, 0
  @@@ hash function called for key: 0, 1, 0, hash: 11
  @@@ hash function called for key: 1, 0, 0, hash: 17   &lt;= why?
  ------------------------------
  NUM BUCKETS: 11
  @@@ hash function called for key: 1, 0, 0, hash: 17   &lt;= why?
  BUCKET 0 size: 1
  BUCKET 6 size: 1
  ------------------------------
  after insert of: 0, 1, 0
  ==================================
  about to insert: 0, 0, 1
  @@@ hash function called for key: 0, 0, 1, hash: 1
  @@@ hash function called for key: 0, 1, 0, hash: 11   &lt;= why?
  ------------------------------
  NUM BUCKETS: 11
  @@@ hash function called for key: 1, 0, 0, hash: 17   &lt;= why?
  BUCKET 0 size: 1
  @@@ hash function called for key: 0, 1, 0, hash: 11   &lt;= why?
  BUCKET 1 size: 1
  BUCKET 6 size: 1
  ------------------------------
  after insert of: 0, 0, 1
  ==================================
  </code></pre>
  <p><a href="http://coliru.stacked-crooked.com/a/e2aa8092e536dc34" rel="noreferrer">Code</a> <em>(same behavior for gcc and clang)</em></p>
  <hr />
  <p><sub><strong>Notes:</strong></sub></p>
  <p><sub>1. Trying the same without the <code>bucket_count</code> parameter for the constructor, calls to hash function become even more excessive, due to rehash. But in the scenario above it seems that there is no rehash and there are no collisions.</sub></p>
  <p><sub>2. Related, but specifically on MSVC: <a href="https://stackoverflow.com/questions/33839092/inserting-to-stdunordered-map-calls-hash-function-twice-in-msvcs-stl-bad-d">Inserting to std::unordered_map calls hash function twice in MSVC++&#39;s STL, bad design or special reason?</a></sub></p>