Static foreach.
Jarrett Billingsley
kb3ctd2 at yahoo.com
Thu Nov 2 14:25:46 PST 2006
I am overjoyed by the variadic templates. This is how variadic functions
_should_ be, and this opens up so many other possibilities as well.
One of the cooler things is the ability to iterate over a tuple with
foreach. So:
void print(T...)(T args)
{
foreach(arg; args)
writef(arg);
writefln();
}
This is great. DMD even unrolls this loop, making it a sort of "static
foreach."
But what _else_ could this tuple/foreach facility be used for?
Let's assume the new "foreaching over a tuple" had to be done with "static
foreach", so the above code would simply have a "static" before the
"foreach". Not a large sacrifice to make, and it also makes it explicit
that the loop is looped at compile time and not at runtime.
Now, since static foreach is no longer a "statement", it can appear where
statements can't, like inside regular templates. Then you can iterate over
More information about the Digitalmars-d
mailing list