Recursive template problem
Max Samukha
samukha at voliacable.com.removethis
Wed Jul 30 02:32:15 PDT 2008
On Wed, 30 Jul 2008 04:39:42 -0400, maelp <mael.primet at 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;
}
More information about the Digitalmars-d
mailing list