for (Something something : setOfSomething) // OK
for (Something const& something : setOfSomething) // OK
for (Something& something : setOfSomething) // ERROR
error: invalid initialization of reference of type 'Something&'
from expression of type 'const Something'
Since when does iterator return const Something
? It should return either Something&
or Something const&
. And since range-based 'for' loop is interpreted like that, I have no plausible explanation for what's going on.
Edit: I'm talking about unordered_set
rather than set
, sorry about this confusion.
You can't mutate the members of a set
because that could violate the set
invariants. So the compiler restricts you to getting const references or copies back out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With