std::unordered_map keeps resulting in error, is this a bug?
<p>So I have this function for hashing internal strings, but when I try to run it Visual Studio 2015 gives me a Debug Assertion Failed! Error:</p>

<pre><code>Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector
Line: 1232

Expression: vector subscript out of range
</code></pre>

<p>Now the first time InternalString gets called I get this error and it breaks on the gStringIdTable.find(sid) line.</p>

<pre><code>static std::unordered_map&lt;StringId, const char*&gt; gStringIdTable;

StringId InternalString(const char* string) {
    StringId sid = std::hash&lt;std::string&gt;()(string);

    std::unordered_map&lt;StringId, const char*&gt;::iterator it = gStringIdTable.find(sid);

    if (it == gStringIdTable.end()) {
        gStringIdTable.insert({sid, string});
    }

    return sid;
}
</code></pre>

<p>I thought maybe it was a problem with the way I was initializing the iterator so I figured I'd try this:</p>

<pre><code>if (gStringIdTable.find(sid) == gStringIdTable.end()) {
    gStringIdTable.insert({sid, string});
}
</code></pre>

<p>But that gives me the same error. Then I thought maybe it had to do with doing the find before the unordered_map was populated with anything so I tried ONLY doing an insert in the function. But that too gave me the same error. I tried converting the const char* to std::string, and then only dealing with strings in the unordered_map at <a href="https://stackoverflow.com/questions/15445397/c-unorderedmap-vector-subscript-out-of-range">this answer's</a> suggestion, but got the same error. I tried using emplace instead of insert, tried using std::make_pair, but all combinations to no avail.</p>

<p>Now, am I missing something obviously wrong, or is there bug somewhere?</p>

<h2>Update</h2>

<p>Okay so here is a compiling version where I still get the error. I started an empty c++ project in visual studio 2015 and added these 3 files to match how it is currently implemented in my project:</p>

<p>main.cc</p>

<pre><code>#include "stringid.h"

const static mynamespace::StringId kSidOne = mynamespace::InternalString("One");

int main(int argc, char *argv[]) {
    return 0;
}
</code></pre>

<p>stringid.cc</p>

<pre><code>#include "stringid.h"
#include &lt;string&gt;
#include &lt;unordered_map&gt;

namespace mynamespace {

static std::unordered_map&lt;StringId, std::string*&gt; gStringIdTable;

StringId InternalString(const char* string) {
    StringId sid = std::hash&lt;std::string&gt;()(string);

    if (gStringIdTable.find(sid) == gStringIdTable.end()) {
        gStringIdTable.emplace(sid, new std::string(string));
    }

    return sid;
}

} // mynamespace
</code></pre>

<p>string.h</p>

<pre><code>#ifndef STRINGID_H_
#define STRINGID_H_

namespace mynamespace {

typedef unsigned int StringId;
StringId InternalString(const char* string);

} // mynamespace

#endif // STRINGID_H_
</code></pre>

<p>I also did some debugging into the functions to see if I could figure out where the problem is arising from and it looked like when the find function grabs the relevant bucket it returns null or 0 and then the _Begin function throws and error because the size is equal to zero. </p>

<h3>Small Update</h3>

<p>I also tried compiling with gcc. It compiles fine, but I still get an error on find().</p>