
On Tue, May 3, 2011 at 11:39 PM, Craig Longman
On 03/05/2011 4:59 PM, Nat Goodspeed wrote:
unordered_map< string, uint32_t > xrf1; unordered_map< string, uint32_t >::const_iterator& iter = xrf1.find( "hello" );
This code makes me shudder.
Perhaps a bit dramatic, but you're quite right. =)
I'm actually honestly not sure where I picked that up with iterators, but it worked and I guess it just kinda stuck. I try and use references over copies where appropriate, and as it seemed to work. I didn't think it through properly.
Please forgive my tone. We have just recently spent (one might even say "wasted") quite a bit of engineering time chasing the distinction between "it works for now" and "it's correct." That difference rears its ugly head when porting to another platform -- or upgrading compilers, in our case to VS2010. At least in theory, correct code should *continue* to work.
To answer your question, because xrf1 isn't const, its find() returns a non-const iterator.
But, the confusion is if I replace unordered_map with std::map, it compiles fine. I guess that means in std::map a const_iterator is just that, a "const iterator", but in unordered_map, the const_iterator is a completely different type, not just a const version of the normal iterator?
Sounds like a plausible guess.
Time to dig through some header files I guess.
To satisfy your curiosity, excellent. But to base your coding decisions on present implementation details, not so much. To paraphrase TV cops, anything not explicitly documented as part of the library interface can and will be used against you.