Search elemnt in Compile-time Argument List of strings

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 26 12:30:18 PDT 2016


I want to generate one function for any struct data member, but 
also want to be able to skip few of the members. The first part 
works, but I have some trouble with the skipping.

I pass the struct type and a Compile-time Argument List of 
strings as template arguments to a template function, list all 
members of the struct and compare each member to each element of 
the List. If the member is in the List skip processing of that 
member. Pretty straight forward ... should be.

// First approach doesn't work:
// Error: variable skip cannot be read at compile time
void processMember( T, ignore... )() {
   foreach( member; __traits( allMembers, T )) {
     bool skip = false;
     foreach( arg; ignore )
       skip = skip || ( arg == member );

     static if( !skip ) {
       // process member here, generate e.g. setter function as 
string mixin
     }
   }
}

// Second approach, get warnings for every skipped member
// and every line after the return statement:
// Warning: statement is not reachable
void processMember( T, ignore... )() {
   foreach( member; __traits( allMembers, T )) {
     foreach( arg; ignore )
       static if( arg == member )
         return;
     // process member here, generate e.g. setter function as 
string mixin
   }
}

So how can I achieve my goal the right way?


More information about the Digitalmars-d-learn mailing list