这个简单的代码会生成一个有关“烘焙指针的对象将在完整表达式末尾被销毁”的警告。这意味着什么?entry使用后可以不使用吗get_map?还有为什么会出现这个警告
static std::map<std::string, int *> get_map() {
static std::map<std::string, int*> the_map;
return the_map;
}
int main() {
(...)
auto entry = get_map().find("HEY");
(...) use entry , is that wrong ?
}
Run Code Online (Sandbox Code Playgroud)
entry使用后可以不使用吗get_map?
你不能。
static std::map<std::string, int *> get_map()
Run Code Online (Sandbox Code Playgroud)
返回地图的副本。
auto entry = get_map().find("HEY");
Run Code Online (Sandbox Code Playgroud)
返回一个指向副本的迭代器。副本在entry分配后立即被销毁(因为副本没有保存在任何变量中,所以它仍然是临时的)。所以,entry不能安全使用。
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |