Simulating rectangular array

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 17 13:01:46 PDT 2012


On Sunday, June 17, 2012 21:50:32 Andrej Mitrovic wrote:
> Has anyone come up with a template that can simulate a rectangular
> array and allow one to override opAssign to do special work? E.g.:
> 
> Array!(2, 4) arr;
> arr[1][3] = "foo";
> 
> This would invoke opAssign in some templated struct "Array". This
> would be super-easy to implement if I had an opIndex that could work
> with the above syntax. I know there's a "[1, 3]" syntax but I really
> need the array "[1][3]" syntax.
> 
> Maybe Philippe has this in his templates book? (If not it would be a
> great addition to it)

I believe that all you'd need to do is make it so that arr[1] returns an 
object which you can do the [3] on and get what you want. If returning an 
array works for that, then there you go. If you need another struct, then 
define the other struct. However, I would point out that you'd need a third 
template argument to Array for the type (unless you want it to hold a 
Variant).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list