Dimensions in compile time
Basile B.
b2.temp at gmx.com
Mon Feb 8 12:26:20 UTC 2021
On Monday, 8 February 2021 at 12:19:26 UTC, Basile B. wrote:
> On Monday, 8 February 2021 at 11:42:45 UTC, Vindex wrote:
>> [...]
>
> yeah.
>
> ---
> template dimensionCount(T)
> {
> static if (isArray!T)
> {
> static if (isMultiDimensionalArray!T)
> {
> alias DT = typeof(T.init[0]);
> enum dimensionCount = dimensionCount!DT + 1;
> }
> else enum dimensionCount = 1;
> }
> else enum dimensionCount = 0;
> }
> ///
> unittest
> {
> static assert(dimensionCount!char == 0);
> static assert(dimensionCount!(string[]) == 1);
> static assert(dimensionCount!(int[]) == 1);
> static assert(dimensionCount!(int[][]) == 2);
> static assert(dimensionCount!(int[][][]) == 3);
> }
> ---
>
> that can be rewritten using some phobos traits too I think, but
> this piece of code is very old now, more like learner template.
well I didn't realize but static array are not handled. I think
this is because in first place this was made for a specific usage
that was a serializer.
More information about the Digitalmars-d-learn
mailing list