Timushev Roman
Timushev Roman

Reputation: 483

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?

Upvotes: 5

Views: 227

Answers (2)

Andrei Alexandrescu
Andrei Alexandrescu

Reputation: 3216

If you already know the type is numeric, an alternative is T1.min == 0.

Upvotes: 7

Chris Cain
Chris Cain

Reputation: 666

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) {
    //...
}

Upvotes: 10

Related Questions