Is it possible to return a non-const iterator from an unordered_set with a custom_key? #2152
-
From the answer here: https://stackoverflow.com/questions/18704129/unordered-set-non-const-iterator I'm using the Microsoft compiler (VS2019 v142 :::: /std:c++17). I create a custom key type for an unordered_set and a hasher for it.
I then call:
This fails to compile with the following message:
double clicking this takes me into the std::generate function specifically
So it's erroring out as I double checked everything my end and I had no const's in my code so something must be turning it const. Commenting out the std::generate I did a localized test and sure enough I get the same issue:
It seems as though unordered_set::iterator uses it's base iterator from std::_Hash which uses:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
As mentioned in the Q&A, non-const element access is intentionally disabled, because modification of elements in a In C++17, it is possible to |
Beta Was this translation helpful? Give feedback.
As mentioned in the Q&A, non-const element access is intentionally disabled, because modification of elements in a
std::unordered_set
will change the elements' hash value and thus disrupt the internal organization ofstd::unordered_set
.In C++17, it is possible to
extract
nodes from astd::unordered_set
, modify theirvalue()
s while they are not stored in a container, and theninsert
them back to the container.