How to implement predicates for Filter!(Pred, Tuple)
Tobias Pankrath
tobias at pankrath.net
Fri Nov 11 02:04:18 PST 2011
I've written a Filter-Template, which implements the well-known function for
template arguments:
-----
template Filter(alias SPred, E...)
{
static if(E.length == 0) {
alias TypeTuple!() Filter;
}
else
{
static if(SPred!(E[0]))
{
alias TypeTuple!(E[0], Filter!(SPred, E[1..$])) Filter;
}
else
{
alias Filter!(SPred, E[1..$]) Filter;
}
}
}
------
This works just fine:
---
template IsEqual(int This)
{
template IsEqual(int That)
{
enum bool IsEqual = This == That;
}
}
static assert(is(Filter!(IsEqual!5, 5,1,5,1,5) == TypeTuple!(5, 5, 5)));
---
However I can't use it directly, for example like this:
> pragma(msg, IsEqual!(5)!5);
However if I wouldn't rely on the ETT, and use a different name for the
inner
template, it will work.
---
template IsEqual(int This)
{
template Pred(int That)
{
enum bool Pred = This == That;
}
}
pragma(msg, IsEqual!(5).Pred!(5));
---
But that will make IsEqual less elegant to use in conjunction with filter.
So, is there a way to elegantly use one template in both cases? Otherwise I
would declare two versions:
one with a single and one with two parameter.
--
Tobias
More information about the Digitalmars-d-learn
mailing list