Search code examples
c++c++11memory-leakssetemplace

How does set::emplace handle objects that are already in a set?


I have a set of objects, and I want to use emplace to add objects to the set. If an equivalent object does not already exist in the set, set::emplace creates an object and puts it in the set. If the set already has an equivalent object, set::emplace does not add an object to the list. In this case, does it create the object and destroy it, create it and leak it, or not create it at all? Or will it do something else?

C++ 11, gcc 4.7.0


Solution

  • It's not supposed to construct the object at all, but it generally will anyway (certainly using libstd++); this is technically a bug, but more so in the standard than anything else.

    Fuller details can be found in a previous answer of mine to a very similar question: