Inability to dup/~ for const arrays of class objects

Ali Çehreli acehreli at yahoo.com
Wed May 29 16:45:04 PDT 2013


On 05/29/2013 03:59 PM, Peter Williams wrote:

 > I've been trying to find out how non ref array arguments are passed to
 > functions in D but can't find any documentation on it.

The following concepts are relevant:

- Dynamic array: Maintained by the D runtime

- Fixed-length array (aka static array): Can be on the stack

- Slice: An efficient tool to access a range of elements (of any type of 
array)

Usually, it is the slice that gets passed:

   void foo(int[] slice);

A slice is made up of the pointer to the first element and the number of 
elements:

struct __SomeImplementationDependentName__
{
     size_t length;
     void * ptr;
}

When you pass a slice by-value, as in the case of foo() above, that 
struct gets copied: a copy of the argument...

So, slice variables have value semantics but they are used as references 
to elements.

Fixed-length arrays are a different story: Unlike C arrays and unlike D 
slices, the elements are always copied.

 > If doing that is not much less efficient than passing by ref (and 
isolates
 > the external representation of the array from anything I do) then I 
can stop
 > using ref and the problem goes away.

Yes, simply pass-by-reference. Not expensive at all. There may be 
surprises though; you may want to read this article:

   http://dlang.org/d-array-article.html

Ali



More information about the Digitalmars-d mailing list