0

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?

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
  • [How to have an unordered_map where the value type is the class it's in?](https://stackoverflow.com/a/13089641/8411406) might help – Turtlefight Aug 30 '19 at 21:15
  • 1
    @Thanks, that's much more directly relevant: there's a special exemption from the rules for the smart pointer types written into the standard. – Daniel McLaury Aug 30 '19 at 21:17

0 Answers0