On 06/01/11 17:49, Max Samukha wrote:
Some of us who have the knack of writing metaprograms in D know that
many algorithms can be implemented with both recursive templates and
CTFE. A simple example is map:

Recursive template instantiation:

template staticMap(alias pred, A...)
{
static if (A.length)
alias TypeTuple!(pred!(A[0]), staticMap!(A[1..$])) staticMap;
}

CTFE:

template staticMap(alias pred, A)
{
mixin("alias TypeTuple!(" ~ _staticMap(A.length) ~ ") staticMap;");
}

private string _staticMap(size_t len)
{
string result;
if (len)
{
result ~= "pred!(A[0])";
for(size_t i = 1, i < len; ++i)
{
result ~= ", pred!(A[" ~ to!string(i) ~ "])";
}
}
return result;
}

It is not easy to decide which approach to implement in a library
because both have drawbacks.

Can anybody give informed advice as to which one is preferable in
practice? Or should a library provide both?

Put each of those implementations in its own file, then call it several times. Look at the binary size for each implementation, also run `strings ctfeTest` if you're on a *nix. This should give you your answer :)

--
Robert
http://octarineparrot.com/

Reply via email to