View mode: basic / threaded / horizontal-split · Log in · Help
July 30, 2008
Re: Recursive template problem
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.

My problem was to construct a struct having k (known at compile time) elements named (for instance)
v1 ... vk,

ie. something like

MyStruct!(5) 

=>

struct
{
 int v1, v2, v3, v4, v5 ;
}

someone could help me with this? Or is it possible to do this?
July 30, 2008
Re: Recursive template problem
On Wed, 30 Jul 2008 04:39:42 -0400, maelp <mael.primet@gmail.com>
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.
>
>My problem was to construct a struct having k (known at compile time) elements named (for instance)
>v1 ... vk,
>
>ie. something like
>
>MyStruct!(5) 
>
>=>
>
>struct
>{
>  int v1, v2, v3, v4, v5 ;
>}
>
>someone could help me with this? Or is it possible to do this?
>
> 

One of a few ways to do that:

import std.stdio;
import std.metastrings;

template Fields(uint n)
{
   static if (n)
   {
       mixin ("int v" ~ ToString!(n) ~ ";");
       mixin Fields!(n - 1);
   }
}

struct S(uint n)
{
   mixin Fields!(n);
}

void main()
{
   S!(5) s;
   s.v1 = 1;
   s.v2 = 2;
   s.v5 = 3;    
}
July 30, 2008
Re: Recursive template problem
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
July 30, 2008
Re: Recursive template problem
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.
Top | Discussion index | About this forum | D home