Does foreach on array literal allocate?

Walter Bright newshound1 at digitalmars.com
Thu Dec 4 19:13:02 PST 2008


Bill Baxter wrote:
>> and note the call to gc_malloc().
> 
> Bummer.

I agree the compiler could do better there. There is a lot of 
opportunity for better optimization. I haven't spent time on it because 
of all the other things that need doing first.


> Does a static-sized array initializer also allocate?
> 
> int[4] v = [1,2,3,4];
> foreach(x; v) {
>     /// ...
> }
> 
> Or maybe this (also) works without allocation?
> 
> foreach(x; Tuple!(1,2,3,4))
> {
>    ///
> }
> 
> This kinda code is useful for the case where you have to do the same
> thing to two or three local variables.  You can write a local function
> to do it, but foreach on a static set of them breaks up the visual
> flow of the code a little less, I think.
> 
> 
> --bb



More information about the Digitalmars-d mailing list