Perhaps a bit dramatic, but you're quite right. =)> unordered_map< string, uint32_t > xrf1; > unordered_map< string, uint32_t >::const_iterator& iter = xrf1.find( "hello" ); > This code makes me shudder. Capturing the value returned from a function with a reference variable asserts that the function is itself returning a reference, not a value - and always will, for the life of the codebase. This is far more general than iterators. My rule of thumb is: return by value, store returned data by value.
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? I was under the impression that const could and would always be implicitly applied if needed, just not the other way round (obviously).To answer your question, because xrf1 isn't const, its find() returns a non-const iterator. You're trying to capture a reference to a const_iterator, which isn't what you have in hand.
Craig Longman
If it's not signed by me, then please don't assume it is from me.