DMD1 function template broken or did I f.u.?

Steven Schveighoffer schveiguy at yahoo.com
Tue Aug 24 08:36:04 PDT 2010


On Tue, 24 Aug 2010 11:18:35 -0400, 0ffh  
<frank at youknow.what.todo.internetz> wrote:

>
> Hi, all!
>
> Try this:
>
> ---< snip >---
>
> void remove(T)(out T[] array,T element) {
>    int r=0,w=0;
>    while (r<array.length) {
>      if (array[r]!=element)
>        array[w++]=array[r];
>      ++r;
>    }
>    array.length=w;
> }
>
> void test() {
>    int[] array;
>    int element=2;
>    //
>    array=[1,3,2,2,1,3,1,1,2];
>    writef("direct\n");
>    writef("  before : ",array,"\n");
>    int r=0,w=0;
>    while (r<array.length) {
>      if (array[r]!=element)
>        array[w++]=array[r];
>      ++r;
>    }
>    array.length=w;
>    writef("  after  : ",array,"\n");
>    //
>    array=[1,3,2,2,1,3,1,1,2];
>    writef("template\n");
>    writef("  before : ",array,"\n");
>    remove!(int)(array,element);
>    writef("  after  : ",array,"\n");
> }
>
> ---< snap >---
>
> I get the following output:
>
> direct
>    before : [1,3,2,2,1,3,1,1,2]
>    after  : [1,3,1,3,1,1]
> template
>    before : [1,3,2,2,1,3,1,1,2]
>    after  : []
>
> So, my question is: Huh?

s/out/ref

out means "return this argument by reference, but initialize it to its  
initial value first" which for arrays means, a null array.

ref means "pass the argument by reference."

Also, btw, you should not need to specifically call the !int version, you  
can just do remove(array, element).

-Steve


More information about the Digitalmars-d-bugs mailing list