Recursive template problem
BCS
ao at pathlink.com
Wed Jul 30 09:52:33 PDT 2008
Reply to Lars,
> maelp wrote:
>
>> I think what he looks for is not the actual result since his array of
>> reals isn't const (not known at compile time), and he simply wants
>> the actual *code* to be generated at compile time, so none of your
>> solution will work. I've had similar problems of recursive template
>> construction, so since there are several posts, maybe we'll have some
>> answers.
>>
> That's right. I am using D for numerical computations, so
> fast-executing code is essential. Therefore, I want to use static
> arrays whenever possible, and instead of having to write
>
> foreach (real r; v) sum += r;
>
> or similar, i would like expressions such as
>
> sum = v[0] + v[1] + ...
>
> to be created at compile time for arrays of any given length. I was
> kinda hoping templates could provide a solution...
>
> -Lars
>
First take a look at the code here, it might give you some ideas:
http://www.dsource.org/projects/scrapple/browser/trunk/bignum/bignum.d
somewhere in there is a hunk of code that generate a tuple like T!(1, 2,
4, 5, ...)
using a foreach over that will let you get compile time enumeration.
Another point to be made is that if you are reading more than just a few
numbers you may actually be ahead to use the foreach as its code will be
much shorter (lower memory usage = better cache usage). As a first guess,
if the unrolled code is more than a memory page long, loop.
More information about the Digitalmars-d
mailing list