When using std::string_view
as a key in a map, are we responsible for making sure the underlying string/char we used to create the std::string_view
is still alive. Reason why I ask this, is because I am seeing very strange behaviour with my map. After I emplace my second key, the first key gets overwritten by the second key.
std::unordered_map<std::string_view, std::unique_ptr<CustomClass> > m_map;
emplace_in_map(std::string_view str, std::unique_ptr<CustomClass> ccc){
m_map.emplace(str, std::move(ccc));
}
wrapper(std::string str) {
// create ccc
emplace_in_map(str, ccc);
}
main(){
wrapper("test1");
wrapper("test2"); // after this test1 key get overwritten by test2
}
After the second call I have a map with two keys that are both equal to test2