1 // Some types which are static arrays. 2 static assert( isStaticArray!(int[12])); 3 static assert( isStaticArray!(const int[42])); 4 static assert( isStaticArray!(inout int[0])); 5 static assert( isStaticArray!(shared(int)[907])); 6 static assert( isStaticArray!(immutable(char)[5])); 7 8 // D doesn't have static array literals, but you get the same effect 9 // by casting a dynamic array literal to a static array, and of course, 10 // the result is typed as a static array. 11 static assert( isStaticArray!(typeof(cast(int[3]) [1, 2, 3]))); 12 13 int[2] sArr = [1, 2]; 14 static assert( isStaticArray!(typeof(sArr))); 15 16 // Some types which are not static arrays. 17 static assert(!isStaticArray!int); 18 static assert(!isStaticArray!(int*)); 19 static assert(!isStaticArray!real); 20 21 static struct S 22 { 23 int[4] arr; 24 } 25 static assert(!isStaticArray!S); 26 27 // The struct itself isn't considered a static array, 28 // but its member variable is when checked directly. 29 static assert( isStaticArray!(typeof(S.arr))); 30 31 // Dynamic arrays. 32 static assert(!isStaticArray!(int[])); 33 static assert(!isStaticArray!(const(int)[])); 34 static assert(!isStaticArray!string); 35 36 int[] arr; 37 static assert(!isStaticArray!(typeof(arr))); 38 39 // A slice of a static array is of course not a static array, 40 // because it's a dynamic array. 41 static assert(!isStaticArray!(typeof(sArr[]))); 42 43 // Static array of dynamic arrays. 44 static assert( isStaticArray!(long[][3])); 45 46 // Dynamic array of static arrays. 47 static assert(!isStaticArray!(long[3][])); 48 49 // Associative array. 50 static assert(!isStaticArray!(int[string])); 51 52 // Of course, null is not considered to be a static array. 53 static assert(!isStaticArray!(typeof(null))); 54 55 enum E : int[3] 56 { 57 a = [1, 2, 3], 58 } 59 60 // Enums do not count. 61 static assert(!isStaticArray!E); 62 63 // This is where isStaticArray differs from __traits(isStaticArray, ...) 64 static assert( __traits(isStaticArray, E)); 65 66 static struct AliasThis 67 { 68 int[] arr; 69 alias this = arr; 70 } 71 72 // Other implicit conversions do not count. 73 static assert(!isStaticArray!AliasThis); 74 75 static assert(!__traits(isStaticArray, AliasThis));
Whether type T is a static array.
Note that this does not include implicit conversions or enum types. The type itself must be a static array. This is in contrast to __traits(isStaticArray, T) which is true for enums (but not for other implict conversions to static arrays).
As explained in the module documentation, traits like this one are not true for enums (unlike most of the __traits traits) in order to avoid testing for implicit conversions by default with template constraints, since that tends to lead to subtle bugs when the code isn't carefully written to take implicit conversions into account.
See also: __traits(isStaticArray, T) The language spec for arrays