CTFE & template predicates

Robert M. Münch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 3 14:46:11 PDT 2015


Hi, I have now played a around couple of hours (reading everything I 
could find) to get something to work, but I think I'm missing some 
basic concepts/understanding. Maybe someone can enlighten me how these 
things work. I thought that some code from David Nadlinger is what I'm 
searching for but don't know how to exactly make use of it.

Here is the code: I want to filter out some of the "allMembers" and use 
them in my code for a switch/final to check that call cases that were 
not filtered are covered. Trying to build the enum for the switch/final 
statement.

auto org_rules = TypeTuple!(__traits(allMembers,BolSource));
static assert(!isTypeTuple!(org_rules));

template startsNotWith(T,char C){
    static if (T[0] != C){
        enum startsNotWith = true;
    } else {
        enum startsNotWith = false;
    }
}

template StaticFilter(alias pred, T...) {
  static if (T.length == 0) {
    alias TypeTuple!() StaticFilter;
  } else static if (pred!(T[0])) {
    alias TypeTuple!(T[0], StaticFilter!(pred, T[1 .. $])) StaticFilter;
  } else {
    alias StaticFilter!(pred, T[1 .. $]) StaticFilter;
  }
}

alias startsNotWithp = startsNotWith!(T,"p"); // doesn't compile: 
Error: undefined identifier T

alias rules = StaticFilter!(startsNotWithp, org_rules);


While playing with this a couple of questions came up:

1. How do predicates get their argument(s)? I saw code where only the 
predicate was mentioned but no arguments. So, I assume there is some 
behind-the-curtain-magic going on. I read about things like "a == b" 
where I can reference 'a and 'b in a string.

2. "enum startsNotwith = false" So this is the return syntax for a CTFE 
for "return(true)" ?

3. TupleType is a very missleading name when you are learning these 
things, because the tuple can hold values as well. Or is there a more 
extensive explanation for the name I don't get?

4. Are there any tutorials about CTFE? This seems to be a very powerful 
but not so easy to use feature of D, but documentation is quite limited.

Thanks a lot.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list