Out of bound problem

bearophile bearophileHUGS at lycos.com
Sat Feb 16 16:43:54 PST 2008


While testing I have found a problem in my code, I have reduced the code to the following lines:

template IsArray(T) {
    const bool IsArray = is(typeof(T.length)) && is(typeof(T.sort)) &&
                         is(typeof(T.reverse)) && is(typeof(T.dup));
}

TA[] foo(TA, TB)(TA[] a, TB b) {
    TA[] result;

    static if (IsArray!(TB)) {
        if (b.length == 0) {
            result = a;
        } else if (b.length == 1) {
            result = foo(a, b[0]);
        }
    }

    return result;
}

void main() {
    foo("test", "");
}


Its output:

bug.d(13): Error: array index 0 is out of bounds b[0 .. 0]
bug.d(13): Error: array index 0 is out of bounds b[0 .. 0]
bug.d(13): Error: array index 0 is out of bounds b[0 .. 0]
bug.d(21): template instance bug.foo!(char,char[0u]) error instantiating

I have found ways to solve this problem, but can someone please explain me what's the problem?

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list