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

About "no const Key" #52

Closed
Philippe91 opened this issue Dec 12, 2022 · 2 comments
Closed

About "no const Key" #52

Philippe91 opened this issue Dec 12, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Philippe91
Copy link

I am aware of this design: "no const Key in std::pair<Key, Value>". I did not care until today.
For a map, this is fine; I mean, willing to change the key is not common. But for a set, the situation is more bug-prone.

For example, this does not compile and this is good:

std::unordered_set<int> set0({ 1, 2, 3 });
for (auto& x : set)
	x++;  // "can't change x"

While the following is compiling fine, hence there is a risk of introducing a subtle bug for a common for-loop scenario.

ankerl::unordered_dense::set<int> set({ 1, 2, 3 });
for (auto& x : set)
	x++;

Hence I am wondering: what is the reason for not being able to have a const key? No way to change that, at least for a set?

@Philippe91 Philippe91 added the enhancement New feature or request label Dec 12, 2022
@martinus
Copy link
Owner

martinus commented Dec 12, 2022

Good point, for unordered_dense::set iterator and const_iterator could be the same type. I'll see what I can do here.

I can't internally use const types though because the map/set has to move elements around. Whenever you delete one entry, the map/set will move an element into the empty place.

@martinus
Copy link
Owner

Fixed in #53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants