www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - AA of static array

reply Derek Parnell <derek psych.ward> writes:
DMD v2.012, Windows

I am trying to use an Associative Array whose data type is a static array
and key type is a string. The declaration and initialization of the array
compiles, but fails with an out of bounds error. If I wrap the static array
inside a struct, it works fine. Here is the code that fails...

void main()
{
   alias int[5] S;

   S[string] foo;
   
   foo["abc"]    = [1,2,3,4,5]; // out of bounds!
   foo["qwerty"] = [9,8,7,6,5];
}

And here is code that works but is kludgy...

void main()
{
   struct S
   {
       int[5] v;
   }

   S[string] x;
   
   x["abc"]   = S([1,2,3,4,5]);
   x["qwerty"]= S([9,8,7,6,5]);
}


Have I coded the first example incorrectly or is this a bug?

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
Mar 07 2008
parent z_axis <z_axis 163.com> writes:
ÔÚ Fri, 07 Mar 2008 21:39:21 +0800£¬Derek Parnell <derek psych.ward> дµÀ:

 void main()
 {
    alias int[5] S;
   S[string] foo;
   foo["abc"]    = [1,2,3,4,5]; // out of bounds!
    foo["qwerty"] = [9,8,7,6,5];
 }
use alias int[] S instead. regards!
Mar 13 2008