Stop function parameters from being copied.

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 7 07:53:20 PDT 2010


On Thu, 07 Oct 2010 10:43:25 -0400, Benjamin Thaut  
<code at benjamin-thaut.de> wrote:

> If I want to tell the compiler that a certain function argument should  
> not be
> copied (say a large struct, or a array) which is the right way to do?
>
> arrays:
> 1. function foo(in float[] bar) { ... }
> 2. function foo(ref const(float[]) bar) { ... }
> 3. something else

Arrays are passed by pseudo-reference.  That is, the data is passed by  
reference, but what part of the data is referred to is passed by value.

so passing an array without any adornments will not copy the array data.

example:

void foo(int[] x)
{
    x = x[3..4]; // does not affect caller's copy
    x[0] = 5; // does affect caller's copy
}

void bar()
{
    int[] x = new int[10];
    foo(x);
    assert(x.length == 10);
    assert(x[3] == 5);
}

> structs:
> 1. function foo(in largestruct bar) { ... }
> 2. function foo(ref const(largestruct) bar) { ... }
> 3. something else

Structs are copied by value, but only a shallow copy.  So if your struct  
is large, you probably want to use ref.  But if your struct is 'large'  
because it contains references to large pieces of data, passing by value  
is ok (similar to arrays).

As a note, in means const scope.  So in largestruct bar is equivalent to  
scope const largestruct bar.  The scope does nothing, so this can be  
reduced to const largstruct bar.  This does not pass by reference.

-Steve


More information about the Digitalmars-d-learn mailing list