Search code examples
d

How can I check, if the type is unsigned or not?


I want check, if the type is byte/short/int/long or it is ubyte/ushort/uint/ulong. The first idea was to use construction is(T1:T2), but is(ulong:long)==is(long:ulong)==true, so it is not the way.

Now I'm using something like is(T:long) && (cast(T)(-1)<cast(T)(1)), but this code seems ugly to me.

So is there more elegant logic statement returning true only if the type is unsigned?


Solution

  • There is a template in std.traits that does what you want: http://dlang.org/phobos/std_traits.html#.isUnsigned

    Use it like:

    if(isUnsigned!T1) {
        //...
    }