In C++, is it undefined behaviour to free memory that is still referred to by a reference, even if that reference is not used again? For example, does the following function invoke undefined behaviour?
void foo() {
std::map<std::string, int> mymap;
{
int& val = mymap["eight"];
val = 8;
mymap.erase("eight"); // Oops! val still refers to mymap["eight"]
// val is not used again
}
std::cout << "Entry count: " << mymap.size() << "\n";
}