Question

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

Was it helpful?

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:

OTHER TIPS

From § 23.2.4 Table 102 — Associative container requirements (in addition to container)

emplace(args)

Effects: Inserts a value_type object t constructed with std::forward<Args>(args)... if and only if there is no element in the container with key equivalent to the key of t.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top