How do I check if a variable is a multidimensional (2D) array?

jfondren julian.fondren at gmail.com
Mon Jul 12 05:08:29 UTC 2021


On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
> I know there is isArray!T and similar functionality in 
> std.traits. But I couldn't find the functionality that can help 
> me check if I have a multidimensional array. Is there any? How 
> do I create my own?
>
> Thanks in advance.

from https://github.com/PhilippeSigaud/D-templates-tutorial

```d
template rank(T) {
     static if (is(T t == U[], U))
         enum size_t rank = 1 + rank!(U);
     else
         enum size_t rank = 0;
}

unittest {
     int a;
     int[] b;
     int[][] c;
     assert(rank!(typeof(a)) == 0);
     assert(rank!(typeof(b)) == 1);
     assert(rank!(typeof(c)) == 2);
}
```

as an example of a recursive template. There's also an 
implementation for ranges.


More information about the Digitalmars-d-learn mailing list