samedi 23 octobre 2021

try_emplace vs. emplace on map / unordered_map

I'm not sure if I have understood try_emplace on map and unordered_map correctly. So please help me. I think that it has the advantage that the constructor of the key or value doesn't get temporarily constructed objects which are themselfes move-constructed from the given key-parameter or value-parameters. This would prevent that if the construction of the key-value-pair inside the map fails the source-objects become empty because their content has aleady moved to the temporary objects. Am I right here ?

And this is done by try_emplace calling emplace with the parameter with either

value_type( piecewise_construct, forward_as_tuple( key ), forward_as_tuple( forward<Args>( args ) ... )

or

value_type( piecewise_construct, move( key ), forward_as_tuple( forward<Args>( args ) ... )

So why does this prevent any conversions on key or args ?

Aucun commentaire:

Enregistrer un commentaire