Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

The return type of functions that use hints should be consistent with std #138

Merged
merged 2 commits into from
Dec 11, 2021
Merged

The return type of functions that use hints should be consistent with std #138

merged 2 commits into from
Dec 11, 2021

Conversation

acd1034
Copy link
Contributor

@acd1034 acd1034 commented Dec 11, 2021

The return types of the member functions that take a hint to insert an element are different between robin_hood::unordered_map and std::unordered_map.
As a result, to use robin_hood::unordered_map, we need to do more than just replace std::unordered_map with robin_hood::unordered_map.
I feel that this is preventing users from using robot_hood::unordered_map.

What I would like to do with this pull request

Make the return type of member functions of robin_hood::unordered_map that take a hint to insert an element consistent with std::unordered_map.

Specific proposals

The member functions of robin_hood::detail::Table that take a hint to insert an element are currently declared as follows.

// robin_hood::detail::Table

// emplace_hint
template <typename... Args>
iterator emplace_hint(Args&&... args);

// insert
std::pair<iterator, bool> insert(const_iterator hint, const value_type& keyval);
std::pair<iterator, bool> insert(const_iterator hint, value_type&& keyval);

// try_emplace
template <typename... Args>
std::pair<iterator, bool> try_emplace(const_iterator hint, const key_type& key, Args&&... args);
template <typename... Args>
std::pair<iterator, bool> try_emplace(const_iterator hint, key_type&& key, Args&&... args);

// insert_or_assign
template <typename Mapped>
std::pair<iterator, bool> insert_or_assign(const_iterator hint, const key_type& key, Mapped&& obj);
template <typename Mapped>
std::pair<iterator, bool> insert_or_assign(const_iterator hint, key_type&& key, Mapped&& obj);

On the other hand, the corresponding functions of std::unordered_map are declared as follows.

Reference: Working Draft, Standard for Programming Language C++ - ISO C++ standards committee

// std::unordered_map

// emplace_hint
template<typename... Args>
iterator emplace_hint(const_iterator position, Args&&... args);

// insert
iterator insert(const_iterator hint, const value_type& keyval);
iterator insert(const_iterator hint, value_type&& keyval);

// try_emplace
template<typename... Args>
iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args);
template<typename... Args>
iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args);

// insert_or_assign
template<typename Mapped>
iterator insert_or_assign(const_iterator hint, const key_type& key, Mapped&& obj);
template<typename Mapped>
iterator insert_or_assign(const_iterator hint, key_type&& key, Mapped&& obj);

I would like to replace the above declaration with the one below.
Some may argue that making the above change makes it impossible to know whether an element was actually inserted or not.
If this slight difference between robin_hood::unordered_map and std::unordered_map is intentional, please abandon this proposal.

Best regards.

Fixes member functions (that take hint) to return iterator instead of std::pair<iterator, bool>.
Fixed member functions are:
- emplace_hint
- insert
- try_emplace
- insert_or_assign
@martinus
Copy link
Owner

Thanks @acd1034 for the contribution! This was a copy & paste error on my side, and not intentional. No idea why the standard just returns an iterator instead of the pair, but it's better to stick with that.

@martinus martinus merged commit 363753c into martinus:master Dec 11, 2021
@acd1034 acd1034 deleted the make-functions-consistent-with-std branch December 11, 2021 13:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants