Search elemnt in Compile-time Argument List of strings

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 26 14:34:28 PDT 2016


On Tuesday, 26 July 2016 at 21:20:18 UTC, ParticlePeter wrote:
...
> First of all there seems to be a typo, it should not be:
>   else static if(i + 1 == arg.length)
>
> ignore must be used instead of arg, as arg.length is the length 
> of a string:
>   else static if(i + 1 == ignore.length)
>
> if ignore is empty, its length is 0, so that the statement 
> would always evaluate to false.
>
> Btw, if ignore is not empty, only the last element (arg) is 
> skipped.


Test:
void processMember( T, ignore... )() {
   foreach( member; __traits( allMembers, T )) {
     foreach( i, arg; ignore ) { // i is the index into the ignore 
tuple
       static if( arg == member ) break; // break out of the 
foreach loop, ...
       else static if( i + 1 == ignore.length ) { // this is the 
last element!
         pragma( msg, "processing ", member );
       }
     }
   }
}

struct Foo { float a, b, c, d; }

int main() {
   processMember!( Foo );            // nada
   processMember!( Foo, "c" );       // works
   processMember!( Foo, "c", "b" );  // skips only b
}





More information about the Digitalmars-d-learn mailing list