Templates, varargs and static foreach

%u user at localhost.com
Sun Jan 14 04:16:52 PST 2007


>There is a "static foreach," only you do not use
>the "static" keyword.

> void foo() {}
>
> void bar(A...)(A a) {
>      foreach(e; a) {
>          foo();
>      }
> }

Dear Kirk, I tried this too, but disassembling to code reveals that the foreach generates extra
instructions when compared to this (dmd 1.00 linux):

 void bar(A...)(A a) {
      static if (a.length>0) foo();
      static if (a.length>1) foo();
      static if (a.length>2) foo();
      static if (a.length>3) foo();
      ...
 }

I tried this without any optimizations. I just want to be sure that the compiler guarantees the
behaviour at compile time. This function is time critical.


More information about the Digitalmars-d-learn mailing list