简体   繁体   中英

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) ?

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.

What matters is that foo1.id() is invoked before the call to emplace , which moves from the reference to foo1 provided by 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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