【发布时间】:2019-06-19 07:41:42
【问题描述】:
考虑以下代码:
static std::unordered_map<std::string, Info> stringCollection;
auto& [it, inserted] = stringCollection.try_emplace(pString);
if (inserted) {
it->second.str = &it->first;
}
return it->second;
这一行it->second.str = &it->first 应该复制密钥(指针)的地址 - 但我似乎无法验证是否会出现这种情况(找不到参考)。基本上it->first给我一份或参考吗?
【问题讨论】:
-
你可以自己猜:如果
if->first是一个值而不是一个引用会发生什么?&it->first将使用临时地址。
标签: c++ c++17 unordered-map