Reputation: 1576
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
Upvotes: 5
Views: 1506
Reputation: 385224
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:
Upvotes: 3
Reputation: 21000
From § 23.2.4 Table 102 — Associative container requirements (in addition to container)
emplace(args)
Effects: Inserts a
value_type
objectt
constructed withstd::forward<Args>(args)...
if and only if there is no element in the container with key equivalent to the key oft
.
Upvotes: 4