0

I'm reading n4860 now and i have some curiosity about this.

i don't know how to explain it so i will just show an example.

now i'm looking "unordered_set" and the draft said

template<class Key,
    class Hash = hash<Key>,
    class Pred = equal_to<Key>,
    class Allocator = allocator<Key>>
class unordered_set;

and i infer that when i make custom Key class then i should make hash, equal_to, allocator class and fill it to container work rightly.

but i can't find what should be filled in classes.

for example, if i make custom Key class and i have to make Hash class with operator(const Key& key) method, and Pred class with operator(const Key& key1, const Key& key2) method.

and another example, Hash class with operator(const Key& key) should return size_t or may be unsigned long int(?) not like string.

i can find that information with web searching and compiler error message, but i can't find on draft.

does draft has no information about like that? or i miss that information because i don't know how to read it?

kaylum
  • 13,833
  • 2
  • 22
  • 31
신승빈
  • 347
  • 3
  • 10
  • 1
    The standard defines the language, but does not teach you how to use it. I would recommend [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Sep 02 '21 at 07:57
  • If needed you must provide template specializations for hash<> and equal_to<> (allocator I think usually is fine). https://en.cppreference.com/w/cpp/utility/hash. – Pepijn Kramer Sep 02 '21 at 07:58
  • then, how book authors and web contributers can know about it? – 신승빈 Sep 02 '21 at 08:05
  • 2
    Here are requirements for hash: https://eel.is/c++draft/utility.requirements#hash.requirements, the predicate should be somewhere in this section as well – Yksisarvinen Sep 02 '21 at 08:12
  • @Yksisarvinen thanks. i should read about it – 신승빈 Sep 02 '21 at 08:25
  • You don't have to provide `Hash`, `Pred` or `Allocator` if `std::hash`, `std::equal_to` and `std::allocator` exist and do what you want. – Caleth Sep 02 '21 at 08:37

1 Answers1

0

The requirements for std::unordered_map are listed in C++20 draft in section 22.2.7. The requirements for Hash are stated in 22.2.7.1.3 as:

Each unordered associative container is parameterized by Key, by a function object type Hash that meets the Cpp17Hash requirements ([hash.requirements]) and acts as a hash function for argument values of type Key, and by a binary predicate Pred that induces an equivalence relation on values of type Key. Additionally, unordered_­map and unordered_­multimap associate an arbitrary mapped type T with the Key.

With [hash.requirements] being in 16.4.4.5 section of this standard.

Pred requirements are in 22.2.7.1.5:

Two values k1 and k2 are considered equivalent if the container's key equality predicate pred(k1, k2) is valid and returns true when passed those values. If k1 and k2 are equivalent, the container's hash function shall return the same value for both.


In general, looking up different requirements in standard is infeasible (unless you are working on a compiler or a resource based on standard like cppreference). It is not really meant for that, because it must be a legal document which takes into account every possibility. I suggest using cppreference instead. Going from page std::unordered_map we can see that it must meet UnorderedAssociativeContainer requirements, which direct us to pages about Hash requirement and to BinaryPredicate requirement.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52