Problem with const correctness

Ali Çehreli acehreli at yahoo.com
Fri Dec 7 13:24:24 PST 2012


  On 12/07/2012 08:42 AM, 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.
 >
 > (But how do you get the original GC-less behaviour if you need max
 > performance and you know what you are doing?)

I've run into the same issue before and have decided that I should just 
forget about T[]...-style parameters and just require an actual array:

     this(T items[])

That is both safe and efficient. If it is really cumbersome, a 
single-item overload can be provided as well:

     this(T item) {
         this([ item ]);    // untested but should work :p
     }

Ali



More information about the Digitalmars-d mailing list