Problem with const correctness

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Dec 7 08:54:47 PST 2012


On Fri, Dec 07, 2012 at 05:42:18PM +0100, bearophile wrote:
> H. S. Teoh:
> 
> >Workarounds:
> >- Use items.dup. Problem: if you're passing an actual array to the
> >  ctor, it's unnecessary and inefficient.
> >
> >- Use an array literal: auto x = Array!int([1,2,3]);, which I believe
> >  should allocate the array on the heap, and so you're safe to just
> >  copy the slice. This defeats the purpose of the "items..." syntax,
> >  though.
> 
> I think the right solution is to fix the design+compiler, because
> it's safer.

Agreed.


> (But how do you get the original GC-less behaviour if you need max
> performance and you know what you are doing?)
[...]

I think there's already an issue open where the compiler should warn you
of escaping reference to stack arguments. So the variadic arguments
syntax will work for the GC-less case, but when you need a GC you have
to explicitly make a heap array, e.g. using an array literal.

The problem comes because there is an ambiguity between runtime stack
slices and heap slices, which are conflated under a single syntax. When
there is no escaping reference, it doesn't matter which two they are,
but when there is, there's a problem.


T

-- 
Microsoft is to operating systems & security ... what McDonalds is to gourmet cooking.


More information about the Digitalmars-d mailing list