The following code compiles without complaint:
struct M
{
std::map<std::string, M> m;
};
The following code, on the other hand, does not:
struct U
{
std::unordered_map<std::string, U> u;
};
We get the following error (after stripping a bunch of template vomit):
In instantiation of ‘struct std::pair<std::string, U>’:
error: ‘std::pair<_T1, _T2>::Second’ has incomplete type
Okay, I can see how letting arbitrary types depend on themselves could lead to problems. But how is it that we run into this error only with certain types and not others? std::map
has the same interface as std::unordered_map
; in particular, it depends on std::pair
as well.
It's not a matter of simply not running into the error conditions with a small amount of code. I can do something like this and it compiles, runs, and produces the expected result.
int main(int argc, char **argv)
{
M m;
m.m["m"] = M();
std::cout << m.m.size() << std::endl;
}
In fact, I'm informed that the U
actually compiles and runs on the Microsoft compiler, just not on gcc or clang.
What is going on here?