Array as an argument, ambiguous behaviour.

Tobias Pankrath tobias at pankrath.net
Wed Jan 29 03:32:33 PST 2014


On Wednesday, 29 January 2014 at 10:55:57 UTC, Cooler wrote:
> Consider 3 functions taking array as an argument:
>
> void fun1(in  int[] x){...}
> void fun2(ref int[] x){...}
> void fun3(    int[] x){...}
>
> auto a = new int[10];
>
> fun1(a); // Guaranteed that "a" will not be changed
> fun2(a); // Guaranteed that we will see any change to "a", made 
> in fun2()
> fun3(a); // Changes to "a" in fun3() may be or may be not 
> visible to the caller
>
> In case of fun3() we have ambiguous behaviour, depending on the 
> body of the function.
>
> Am I right?
> Is that intentional?

Arrays are a pair of pointer and length.

fun1 marks the pointer, the length and the data pointed as const.
fun3 marks nothing const, but since pointer and length are passed 
by value, you'll only see changes to the content of x.
fun2 is like fun3, but pointer and length are itself passed by 
reference.


More information about the Digitalmars-d-learn mailing list