Strange behaviour of var

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Nov 10 15:16:21 PST 2011


size_t can easily bite you* in the ass:

import std.algorithm;

void main()
{
    int[] a;
    int val = max(0, a.length - 1);
    assert(val > 0);  // NG, woops!
}

* = me

That one is easy to catch, but in a complex expression you might have
ints and size_t and  everything gets converted to unsigned, and then
you end up with some value over int.max  which is implicitly assigned
to an int. :/


More information about the Digitalmars-d-learn mailing list