Static foreach.

Walter Bright newshound at digitalmars.com
Sat Nov 4 01:32:25 PST 2006


Nick wrote:
> Here's a useful example (I think):
> 
> void rotLeft(X...)()
> {
>   typeof(X[0]) tmp = X[0];
>   foreach(int i, x; X[1..$])
>     X[i] = x;
>   X[$-1] = tmp;
> }
> 
> void main()
> {
>   int a=1, b=2, c=3;
>   rotLeft!(a,b,c); // Should produce a,b,c = 2,3,1
> }
> 
> Compiling gives the error "Integer constant expression expected instead of i".
> 
> I'm using this right now, but instead of being generic, the version I have
> contains lots statements like
> ...
> static if(X.length > 2) X[1] = X[2];
> static if(X.length > 3) X[2] = X[3];
> etc.

Template tuple parameters aren't function parameters that can be 
modified. You'll need to pass a, b, c as inout parameters to the 
function, not the template.



More information about the Digitalmars-d mailing list