writing iterators without code duplication. inout?

pompei2 pompei2 at gmail.com
Wed Dec 21 08:30:29 PST 2011


> Just remove the non-const overload. const member functions work 
> with mutable, immutable and const receivers.

Doing this, it compiles but it doesn't do what it should in two 
ways:

1. I leave the code as-is. It compiles but "e" in the first 
foreach loop in "main" is a copy, not a reference. This shows by 
the second loop still displaying the original values, not the +1 
values.

2. To fix 1., I can change the "i" in the foreach loop in 
"doTheIter" to "ref i", which will allow the first foreach loop 
in "main" to successfully modify the array elements. But now, add 
the line "e++;" into the second loop and it still compiles! 
Change it to "ref e" (still in the second loop) and add a third 
loop and it actually does modify the elements! It shouldn't be 
allowed to, as "cc" is const and const is supposed to be 
transitive. I'm not even using a cast.

So either this is not the solution, or I'm doing it wrong.


More information about the Digitalmars-d-learn mailing list