To save time ,just get to my point:I do not understand why such code below
inside the switch ... case statement is allowed ,what is the point of do ...
while...:
This example uses a mixin to implement a generic Duff's device for an arbitrary
statement (in this case, the arbitrary statement is in bold). A nested function
is generated as well as a delegate literal, these can be inlined by the
compiler:
template duffs_device(alias id1, alias id2, alias s)
{
void duff_loop()
{
if (id1 < id2)
{
typeof(id1) n = (id2 - id1 + 7) / 8;
switch ((id2 - id1) % 8)
{
case 0: do { s();
case 7: s();
case 6: s();
case 5: s();
case 4: s();
case 3: s();
case 2: s();
case 1: s();
} while (--n > 0);
}
}
}
}
void foo() { writefln("foo"); }
void test()
{
int i = 1;
int j = 11;
mixin duffs_device!(i, j, delegate { foo(); } );
duff_loop(); // executes foo() 10 times
}
Thanks in advance.
Regards,
Sam