简体   繁体   中英

Inserting heavy objects into std::map

How many times exactly copies the VeryHeavy(args...) in this code?

map.emplace(std::pair<VeryHeavyKey, VeryHeavy>(key, VeryHeavy(args...)));

Or, maybe, there is better to use std::make_pair ? Are there any standartized warranties on copying objects? What's the right way to insert heavy objects into std::map without copying?

What's the right way to insert heavy object into std::map without copying?

prior to C++17

map.emplace(std::piecewise_construct,
              std::forward_as_tuple(std::move(key)),
              std::forward_as_tuple(args...));

post C++17

map.try_emplace(std::move(key), args...);

The C++17 variant improves on the former by not constructing a VeryHeavy if key is already present.

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