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
range-based for loops #49
Comments
The hash map stores Regarding the iterator, it can't just return a mutable reference to I don't know if there is a better way to do it without invoking undefined behavior. I have to store |
Thanks for the explanation. Yes, returning a mutable key doesn't sound like a good idea... My naive approach would be to store the data as |
From what I understand it would be an undefined behavior, see this Stack Overflow post. It would probably work on most compilers but I'd like to avoid any undefined behavior in the code. I have to check a bit how other open-adressing implementations manage this problem. |
That's interesting... Then the only thing I can think of is to represent the value as |
That would be undefined behaviour, if you're storing a |
I'll close the issue as I think the current solution is the safest, but if someone come with a better way, feel free to comment. |
This is a minor thing, but it's been bugging me for a little while. One of the differences with
std::unordered_map
is thatvalue_type
isconst std::pair<Key,T>
instead ofstd::pair<const Key,T>
. Iterators allow us to work around this with thevalue()
method, which gives us a mutable reference to the mapped type.This works fine with traditional for loops and iterators.
However, range-based for loops don't work as nicely, and AFAIK the only way to get a mutable reference to the mapped type in that case is to
const_cast
it:I'm sure there's a reason why
value_type
has to be const (sorry if I missed it), but it's a bit unfortunate that we can't use range-based for loops as straightfordwardly as withstd::unordered_map
. Apart from that, great library! 👍The text was updated successfully, but these errors were encountered: