class TestingMapClass {
public:
TestingMapClass() : anotherStr("nothing") {}
void Load() {
std::unordered_map<std::string, std::string> memberMap = {
{"name", "value"}
};
// Do something with the unordered map...
anotherStr = memberMap["name"]; // The address of the local variable may escape the function
}
private:
std::string anotherStr;
};
int main() {
std::string testingStr;
{
std::unordered_map<std::string, std::string> someMap = {
{"one", "first"}
};
testingStr = someMap["one"];
}
std::cout << testingStr << std::endl; /// Local variable 'testingStr' may point to the memory which is out of scope
return 0;
}
I'm using CLion 2021.2.1 and I get these kind of 'warnings' here:
('The address of the local variable may escape the function'. 'Local variable 'testingStr' may point to the memory which is out of scope.')
From cppreference.com std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[] indeed 'Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.'
But does it really matter actually? Don't anotherStr and testingStr both contain a copy of their respective unordered map's key's values (since the addresses of the strings and their respective unordered map's key's values seem to be different)?