Referencing structs in a foreach

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 22 18:41:32 PDT 2014


On Wednesday, 23 July 2014 at 01:31:48 UTC, Alf wrote:
>   foreach (ref f; [bar.f1, bar.f2])

This allocates new array and copies struct values to it (as D 
structs are value types). I think this should have been a 
compile-time error btw, it never makes sense to do ref iteration 
over an array literal for this very reason.

Most likely you want to forced loop unrolling here and thus 
something like this:

>   import std.typetuple;
>   foreach (ref f; TypeTuple!(bar.f1, bar.f2))

(don't pay attention to the weird "TypeTyple" name, it lies)


More information about the Digitalmars-d mailing list