Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make BijectiveMap compile on more machines by avoiding const #786

Merged
merged 2 commits into from
Feb 3, 2025

Conversation

haved
Copy link
Collaborator

@haved haved commented Feb 3, 2025

When I originally wrote BijectiveMap, I wrote the iterator interface as a thin wrapper around the forward mapping's cbegin() and cend().

The iterator you get from cbegin() has the reference type const std::pair<const Key, Value> &.
To achieve some symmetry, I added an additional const to get const std::pair<const K, const V> &.

Later it turned out that a std::pair or std::unordered_map with added const behaves very unergonomically. When the STL gives a std::pair<const Key, Value> it almost feels like an ugly hack to make only the first immutable. Trying to add additional const for symmetry only makes the assignment operator break in weird ways, and only on some systems.

In practice all I want is for the iterator to return const std::pair<Key, Value> &, but there is no subtyping of these ugly types, so the best I can do without extremely cursed and maybe illegal casting is const std::pair<const Key, Value> &.

@haved haved requested a review from phate February 3, 2025 08:21
@sjalander sjalander enabled auto-merge (squash) February 3, 2025 19:35
@sjalander sjalander merged commit 9b85bdf into master Feb 3, 2025
12 checks passed
@sjalander sjalander deleted the BijectiveMap-fix branch February 3, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants