C++ Logo

std-proposals

Advanced search

[std-proposals] Introduction of value_equal to std::unordered_map / std::unordered_multimap

From: LoS <aurumpuro_at_[hidden]>
Date: Mon, 10 Apr 2023 17:59:17 +0200
Introduction of value_equal to
std::unordered_map / std::unordered_multimap

I. Motivation
The std::map / std::multimap containers already provides a value_compare
member class that
allows to compare two value_type objects using the key_compare comparison
object under
the hood. This allows the user to directly call an instance of the
value_compare comparison
object and thus perform a compare operation between two value_type objects
easily.
However, it is not possible to do such a thing with the std::unordered_map
/
std::unordered_multimap containers, because they do not provide a member
class that allows to
compare two value_type objects in an equivalence relation using the
key_equal object under
the hood. Besides not being very consistent with the associative containers
interface, this lack forces
the user to always call an instance of key_equal and then pass the key part
of the elements to the
function object, or forces the user to implement his own lambda function or
class funcrion that does
the same thing under the hood.
For consistency, if the std::unordered_map / std::unordered_multimap
containers provide a member
class value_equal also the std::unordered_set / std::unordered_multiset
containers need to
provide a member type value_equal, that is, an alias for key_equal.

II. Impact On the Standard
The proposal adds a new member class in the std::unordered_map /
std::unordered_multimap
containers and a new member type in the std::unordered_set /
std::unordered_multiset containers. It
does not require any changes in the core language. It has been implemented
in standard C++.

III. Design Decisions
The std::unordered_map / std::unordered_multimap containers store the
public member class
value_equal. The member class is implemented depending on the following
specifications:

equal
(protected member type)

It is an instance of key_equal.
value_equal(key_equal);
(protected member function)

It takes an key_equal object and initializes the stored object with it.
It may throw implementation-defined exceptions.
bool operator()(const value_type&, const value_type&) const;
(public member function)
It takes two value_type objects and returns true if lhs.first compares
equal to rhs.first according
to the stored object. It effectively calls equal(lhs.first, rhs.first).
It may throw implementation-defined exceptions.

The std::unordered_set / std::unordered_multiset containers have the public
member type
value_equal, that is, an alias for key_equal.

Received on 2023-04-10 15:59:32