1 module nxt.array_traits; 2 3 /// Is `true` iff `T` is a slice of `char`s. 4 enum isCharArray(T) = (is(T : const(char)[])); 5 6 /// 7 @safe pure unittest 8 { 9 static assert(isCharArray!(char[])); 10 static assert(isCharArray!(const(char[]))); 11 static assert(isCharArray!(const char[])); 12 static assert(isCharArray!(const const(char)[])); 13 14 static assert(!isCharArray!(wstring)); 15 static assert(!isCharArray!(dstring)); 16 } 17 18 /** Is `true` iff all `Ts` are slices with same unqualified matching element types. 19 * 20 * Used to define template-restrictions on template parameters of only arrays 21 * (slices) of the same unqualified element types. 22 */ 23 template isSameSlices(Ts...) 24 if (Ts.length >= 2) 25 { 26 enum isSlice(T) = is(T : const(E)[], E); 27 enum isSliceOf(T, E) = is(T : const(E)[]); 28 static if (isSlice!(Ts[0])) 29 { 30 alias E = typeof(Ts[0].init[0]); 31 static foreach (T; Ts[1 .. $]) 32 { 33 static if (is(typeof(isSameSlices) == void) && // not yet defined 34 !(isSliceOf!(T, E))) 35 { 36 enum isSameSlices = false; 37 } 38 } 39 static if (is(typeof(isSameSlices) == void)) // if not yet defined 40 { 41 enum isSameSlices = true; 42 } 43 } 44 else 45 { 46 enum isSameSlices = false; 47 } 48 } 49 50 /// 51 @safe pure unittest 52 { 53 static assert(isSameSlices!(int[], int[])); 54 static assert(isSameSlices!(const(int)[], int[])); 55 static assert(isSameSlices!(int[], const(int)[])); 56 static assert(isSameSlices!(int[], immutable(int)[])); 57 58 static assert(isSameSlices!(int[], int[], int[])); 59 static assert(isSameSlices!(int[], const(int)[], int[])); 60 static assert(isSameSlices!(int[], const(int)[], immutable(int)[])); 61 static assert(isSameSlices!(const(int)[], const(int)[], const(int)[])); 62 63 static assert(!isSameSlices!(int, char)); 64 static assert(!isSameSlices!(int, const(char))); 65 static assert(!isSameSlices!(int, int)); 66 67 static assert(!isSameSlices!(int[], char[])); 68 static assert(!isSameSlices!(int[], char[], char[])); 69 static assert(!isSameSlices!(char[], int[])); 70 71 static assert(!isSameSlices!(char[], dchar[])); 72 static assert(!isSameSlices!(wchar[], dchar[])); 73 static assert(!isSameSlices!(char[], wchar[])); 74 }