Traits: getting all *public* members of a class?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Jun 8 07:06:31 PDT 2013


On Saturday, 8 June 2013 at 13:57:13 UTC, Q wrote:
> Hi there, I want to iterate over all the public members of a 
> class.

It is a known problem. The workaround is to use is(typeof()):

void main() {
     foreach (mem; __traits(allMembers, Foo))
     {
         static if (is(typeof(
             __traits(getMember, Foo.init, mem)
         )))
         {
             enum prot = __traits(getProtection,
                             __traits(getMember, Foo.init, mem));
             static if (prot == "public") {
                 pragma(msg, mem);
             }
         }
     }
}


More information about the Digitalmars-d-learn mailing list