简体   繁体   English

在地图插入过程中C ++移动保证

[英]C++ move guarantees during map insertion

I have a code snippet which looks somewhat like this: 我有一个代码片段,看起来有点像这样:

std::unordered_map<FooId, Foo> fooMap;
Foo foo1(..);
fooMap.emplace(foo1.id(), std::move(foo1));

Is the emplace safe, ie does the C++ language standard guarantee that foo1.id() is invoked before std::move(foo1) ? 安全性是否安全,即C ++语言标准是否保证在std::move(foo1)之前调用foo1.id() std::move(foo1)

You're asking the wrong question: std::move does nothing, so it doesn't matter that the function arguments aren't evaluated in a specified order. 你问的是错误的问题: std::move什么都不做,所以函数参数不按指定的顺序进行评估也没关系。

What matters is that foo1.id() is invoked before the call to emplace , which moves from the reference to foo1 provided by std::move . 重要的是, foo1.id()被调用在调用之前emplace ,从基准移动到foo1所提供std::move That is the case - function calls are always sequenced after the evaluation of their arguments. 就是这种情况 - 在评估参数后,函数调用总是被排序。

This code is safe as long as id() returns a value, not a reference to something that might be destroyed or invalidated by the move. 只要id()返回一个值,而不是对可能被移动销毁或无效的内容的引用,此代码就是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM