enum detection

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Nov 22 07:31:11 PST 2012


On 11/22/12, Jack Applegame <japplegame at gmail.com> wrote:
> How to detect enum member?
>
> struct A {
>    enum id = 10;
>    int b;
>    char c;
> }
> foreach(ident; __traits(allMembers, A)) {
>    // is ident enum or not?
> }

That's not an enum, that's a manifest constant. This is an enum:
struct A {
enum id { x = 10 }
}

You can use:

struct A
{
    enum id = 10;
    int b;
    char c;
}

template isEnum(alias symb)
{
    static if (is(symb == enum))
        enum bool isEnum = true;
    else
        enum bool isEnum = false;
}

void main()
{
    foreach(ident; __traits(allMembers, A))
    {
        static if (isEnum!(__traits(getMember, A, ident)))
        {
            pragma(msg, ident, " yes");
        }
    }
}


More information about the Digitalmars-d-learn mailing list