identic ref/inout parameter

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Sep 15 07:40:24 PDT 2007


"Downs" <default_357-line at yahoo.de> wrote in message 
news:fcetv4$6ii$1 at digitalmars.com...

> void assertUnique(T...)(ref T tuple) {
>  foreach (index, ref v1; tuple[0..$-1])
>    foreach (ref v2; tuple[index+1..$])
>      assert(&v1 != &v2);
> }

Hm.  It doesn't compile with the 'ref' for the foreach loop values, but when 
you take those out, it doesn't work.  It works if you just index the tuple 
instead, though:

void assertUnique(T...)(ref T tuple)
{
    foreach(i1, v1; tuple[0 .. $ - 1])
        foreach(i2, v2; tuple[i1 + 1 .. $])
            assert(&tuple[i1] != &tuple[i1 + 1 + i2]);
}

Sadly, this can't be used in a function precondition because it gives the 
error "cannot modify parameter in precondition".  The compiler thinks you're 
trying to modify the parameters when you pass them to assertUnique.  (A case 
where "const ref" would be useful!  How about that.)  But you can still call 
it at the beginning of the function. 




More information about the Digitalmars-d-learn mailing list