Why does the following code crash both on Visual Studio and GCC?
For it to crash it requires the range-based for loop, std::map, std::string and taking a reference t
S().func()
This constructs a temporary object, and invokes a method that returns a reference to a std::string that's owned (indirectly) by the temporary object (the std::string is in the container that's a part of the temporary object).
After obtaining the reference, the temporary object gets destroyed. This also destroys the std::string that was owned (indirectly) by the temporary object.
After that point, any further usage of the referenced object becomes undefined behavior. Such as iterating over its contents.
This is a very common pitfall, when it comes to using range iteration. Yours truly is also guilty of getting tripped over this.