How to determine constness at compile time?

Janice Caron caron800 at googlemail.com
Tue Nov 27 05:00:46 PST 2007


I'm trying to write a template to determine the constness (or not) of
array members. The following seemed the obvious thing to do, but it
doesn't work. Anyone know how to do it right?

template constID(T)
{
    static if(is(T:invariant(T)[]))
    {
        static const int constID = 2;
    }
    else static if(is(T:const(T)[]))
    {
        static const int constID = 1;
    }
    else
    {
        static const int constID = 0;
    }
}

void main()
{
    writefln(constID!(string));
}

This prints 0. I would have expected (hoped for) it to print 2.



More information about the Digitalmars-d mailing list