array depth template

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jun 11 19:16:18 PDT 2009


On Thu, Jun 11, 2009 at 9:15 PM, Saaa<empty at needmail.com> wrote:
> Is this a good way to get the depth of an array?
>
> int getArrayDepth(T)(ref T array)
> {
> static if( is(T A:A[]) )
> {
> A arr;
> return 1 + getArrayDepth(arr);
> }
> else
> {
> return 0;
> }
> return -1;
> }

It's kind of the right idea, but.. it's also kind of weird.

template ArrayDepth(T: T[]) { const ArrayDepth = 1 + ArrayDepth!(T); }
template ArrayDepth(T)       { const ArrayDepth = 0; }

This lets you get the depth of any array _type_, like
ArrayDepth!(int[][]) gives 2.


More information about the Digitalmars-d-learn mailing list