Reputation: 115
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?
Upvotes: 2
Views: 105
Reputation: 62636
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.
Upvotes: 6