std::unordered_map::emplace object creation
In my opinion, the quoted part from the standard is misleading, because it suggests, that the object is only constructed if there is no matching element in the container. I guess they are trying to state:
Effects: Constructs a
value_type
objectt
withstd::forward<Args>(args)...
. Inserts the constructed objectt
if and only if there is no such element in the container with key equivalent to the key oft
.
The reason is: The implementation of the function emplace
has to construct t
in order to find out if an element with an equivalent key exists, because the implementation has to invoke the hash function and the equals predicate. However, in general they can only be invoked with objects of type value_type
, not with tuples used to construct these objects.
In theory, it would be possible to specify an emplace function, that doesn't construct t
if there is already an element with an equivalent key. Interestingly, something similar will be added with C++14 for std::map::find
. See the following documentation:
- http://en.cppreference.com/w/cpp/container/map/find
There are two overloads, that can be used with arbitrary types, as long as the compare function fulfills some additional requirements. Interestingly enough, there is no such overload for std::unordered_map
.