Another interesting hack: expand a static array into parameter arguments
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Apr 3 06:38:16 PDT 2014
On 4/3/14, Artur Skawina <art.08.09 at gmail.com> wrote:
> Actually, they are *much easier* to debug than the recursive templates
> -- because you can always look at the generated code, something that
> is impossible when using the templates.
Personally I think we need a third mechanism. I would totally love to
be able to write CTFE-style template code. Here's a demonstration:
-----
template FilterInts(Args...)
{
foreach (T; Args)
{
static if (is(T == int))
FilterInts ~= T; // populate a type tuple
}
}
void main()
{
static assert(is(FilterInts!(int, float, int, string) ==
TypeTuple!(int, int)));
}
-----
Or another one:
-----
template GetFirstArray(Args...)
{
foreach (T; Args)
{
static if (isArray!T)
{
GetFirstArray = T;
break;
}
}
}
void main()
{
static assert(is(GetFirstArray!(int, int[], float, float[]) == int[]));
}
-----
This is so much better and easier to understand than recursive
templates. Imperative-style type extraction/manipulation rather than
writing hard-to-grok recursive templates. Thinking about it, I think
the only reason why recursive templates are a popular approach is
because of C++ heritage. But I really think we can do better.
More information about the Digitalmars-d
mailing list