Introduction to traits (and __traits)

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Fri Aug 30 13:57:28 PDT 2013


On 30/08/13 21:40, Benjamin Thaut wrote:
> You need to put it into a static if, otherwise the compiler will continue
> semantic checks on the second part of the expression. E.g.
>
> template isGraph(G)
> {
>    static if(__traits(hasMember, G, "directed"))
>      enum bool isGraph = isBoolean!(typeof(G.directed));
>    else
>      enum bool isGraph = false;
> }

Ahh, right, thanks. :-)

Is there a recommended way for handling the case where there are many such 
members -- say about 10 or more?  The static if's could become very highly 
nested with this approach.  I suppose I could go through like this:

     static if(!__traits(hasMember, G, "one"))
         enum bool isGraph = false;
     else static if(!__traits(hasMember, G, "two"))
         enum bool isGraph = false;
     else static if ...
         ...
     else
     {
         // Now I know all the members exist and I can
         // start checking out their properties ...
     }



More information about the Digitalmars-d-learn mailing list