Out of bound problem
Denton Cockburn
diboss at hotmail.com
Sat Feb 16 18:34:04 PST 2008
On Sat, 16 Feb 2008 19:43:54 -0500, bearophile wrote:
> 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
I tried looking at this. It's weird (and tripping me out).
I would love to see the explanation of what's going wrong here.
More information about the Digitalmars-d-learn
mailing list