0

I was wondering, why does this code compile just fine with g++,

struct FileSystemMap {
    std::map<std::string, FileSystemMap> directories;
    std::map<std::string, std::string> files;
};

while this other doesn't?

struct FileSystemMap {
    std::unordered_map<std::string, FileSystemMap> directories;
    std::unordered_map<std::string, std::string> files;
};
janeD
  • 21
  • 1
  • Please show the verbatim error message you get. – cigien Oct 05 '21 at 19:30
  • Because the error blob I get is so long (7 Kb) that stackoverflow doesn't let me paste it (it tells me "It looks like your post is mostly code; please add some more details."). You should just try my two lines of code. – janeD Oct 05 '21 at 19:40
  • Fair enough, template error messages can be long. Basically, both your snippets are invalid, regardless of whether they produce an error or not. The linked target covers this; it's UB in general, unless a container explicitly says it's allowed. Neither `map` nor `unordered_map` allow it. – cigien Oct 05 '21 at 19:43
  • some implementations of unordered map designed the way that they abstract themselves from contained type by type erasure. Which clearly impossible with `map`. It's better to employ type erasure pattern yourself for unordered and use PIMPL if needed , for ordered.. – Swift - Friday Pie Oct 05 '21 at 23:29

0 Answers0