On Sunday, February 26, 2012 20:57:10 Daniel Murphy wrote: > "Peter Alexander" <[email protected]> wrote in message > news:[email protected]... > > > Perhaps the loop variable should be implicitly const so that this type of > > thing won't happen? > > Making it const would break heaps of valid use cases. It should probably be > an rvalue, and so should the index. I think there's a discussion about > this burried somewhere in bugzilla.
The index bit is highly debatable, as the discussion on that shows (personally, I tend to favor leaving it as-is, because it's useful that way), but regardless making either an rvalue wouldn't make any sense, because they have to be variables that you can operate on in the loop. Doing anything else would seriously break the semantics of foreach. The question is simply whether they should be actual copies so that you don't end up with them altering anything outside of the loop unless you mark them as ref or if the type itself is a reference type. The element is pretty much its own copy regardless, unless you mark it as ref, whereas the situation with the index is more complicated (as the discussion on that shows). - Jonathan M Davis
