Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
* $(LREF PointerTarget)
* $(LREF KeyType)
* $(LREF ValueType)
* $(LREF DimensionCount)
* $(LREF Unsigned)
* $(LREF Largest)
* $(LREF Signed)
Expand Down Expand Up @@ -5920,6 +5921,29 @@ unittest
ValueType!Hash num = 1; // num is declared as int
}

/**
* Returns the dimension count of a static or dynamic array.
* Dimensions are counted up to the first non-array element type.
*/
enum DimensionCount(T : E[], E) = DimCountImpl!E + 1;

///
unittest
{
static assert(DimensionCount!(int[]) == 1);
static assert(DimensionCount!(int[][]) == 2);
static assert(DimensionCount!(int[1][ ]) == 2);
static assert(DimensionCount!(int[ ][1]) == 2);
static assert(DimensionCount!(int[1][1]) == 2);
static assert(DimensionCount!(int[][]*[]) == 1);

// can only be called on arrays
static assert(!__traits(compiles, DimensionCount!int));
}

private enum DimCountImpl(T : E[], E) = DimCountImpl!E + 1;
private enum DimCountImpl(T) = 0;

/**
* Returns the corresponding unsigned type for T. T must be a numeric
* integral type, otherwise a compile-time error occurs.
Expand Down