| 1 |
alias AliasSeq(T...) = T; |
| 2 |
|
| 3 |
struct Holder(T, ubyte val) |
| 4 |
{ |
| 5 |
T x = val; |
| 6 |
} |
| 7 |
|
| 8 |
struct SArrayHolder(T, ubyte val) |
| 9 |
{ |
| 10 |
T[2] x = val; |
| 11 |
} |
| 12 |
|
| 13 |
static foreach (T; AliasSeq!(bool, byte, short, int, long, |
| 14 |
ubyte, ushort, uint, ulong, |
| 15 |
char, wchar, dchar, |
| 16 |
float, double, real)) |
| 17 |
{ |
| 18 |
static assert(__traits(isZeroInit, T) == (T.init is T(0))); |
| 19 |
static assert(__traits(isZeroInit, T[2]) == (T.init is T(0))); |
| 20 |
|
| 21 |
static assert(!__traits(isZeroInit, Holder!(T, 1))); |
| 22 |
static assert(__traits(isZeroInit, Holder!(T, 0))); |
| 23 |
|
| 24 |
static assert(__traits(isZeroInit, SArrayHolder!(T, 0))); |
| 25 |
static assert(!__traits(isZeroInit, SArrayHolder!(T, 1))); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
static assert(__traits(isZeroInit, void)); // For initializing arrays of element type `void`. |
| 30 |
static assert(__traits(isZeroInit, void*)); |
| 31 |
static assert(__traits(isZeroInit, void[])); |
| 32 |
static assert(__traits(isZeroInit, float[])); |
| 33 |
static assert(__traits(isZeroInit, Object)); |
| 34 |
class C1 : Object |
| 35 |
{ |
| 36 |
int x = 1; |
| 37 |
} |
| 38 |
static assert(__traits(isZeroInit, C1)); // An Object's fields are irrelevant. |
| 39 |
|
| 40 |
struct S1 |
| 41 |
{ |
| 42 |
int[] a; |
| 43 |
int b; |
| 44 |
} |
| 45 |
static assert(__traits(isZeroInit, S1)); |
| 46 |
|
| 47 |
struct S2 |
| 48 |
{ |
| 49 |
alias H = Holder!(int, 1); |
| 50 |
H h; |
| 51 |
int a; |
| 52 |
} |
| 53 |
static assert(!__traits(isZeroInit, S2)); |
| 54 |
|
| 55 |
struct S3 |
| 56 |
{ |
| 57 |
S1 h; |
| 58 |
float f = 0; |
| 59 |
} |
| 60 |
static assert(__traits(isZeroInit, S3)); |
| 61 |
|
| 62 |
struct S4 |
| 63 |
{ |
| 64 |
S2 h = S2(S2.H(0), 0); |
| 65 |
int a; |
| 66 |
} |
| 67 |
static assert(__traits(isZeroInit, S4)); |
| 68 |
|
| 69 |
struct S5 |
| 70 |
{ |
| 71 |
Object o = null; |
| 72 |
} |
| 73 |
static assert(__traits(isZeroInit, S5)); |
| 74 |
|
| 75 |
template Vector(T) { alias __vector(T) Vector; } |
| 76 |
static if (is(Vector!(int[4]))) |
| 77 |
{ |
| 78 |
static assert(__traits(isZeroInit, Holder!(Vector!(int[4]), 0))); |
| 79 |
static assert(!__traits(isZeroInit, Holder!(Vector!(int[4]), 1))); |
| 80 |
} |
| 81 |
|
| 82 |
// https://issues.dlang.org/show_bug.cgi?id=24776 |
| 83 |
struct S6 { |
| 84 |
union { |
| 85 |
int i1; |
| 86 |
float f1; |
| 87 |
} |
| 88 |
} |
| 89 |
static assert(__traits(isZeroInit, S6)); |
| 90 |
|
| 91 |
struct S7 |
| 92 |
{ |
| 93 |
union { |
| 94 |
float f2; |
| 95 |
int i2; |
| 96 |
} |
| 97 |
} |
| 98 |
static assert(!__traits(isZeroInit, S7)); |
| 99 |
|
| 100 |
// https://issues.dlang.org/show_bug.cgi?id=23841 |
| 101 |
union U |
| 102 |
{ |
| 103 |
float x = 0; |
| 104 |
float y; |
| 105 |
} |
| 106 |
static assert(__traits(isZeroInit, U)); |
| 107 |
|
| 108 |
union U2 |
| 109 |
{ |
| 110 |
float x; |
| 111 |
int y; |
| 112 |
} |
| 113 |
static assert(!__traits(isZeroInit, U2)); |
| 114 |
|
| 115 |
struct S8 { |
| 116 |
int[0] dummy; // same offset as anon union, but doesn't overlap; should be ignored anyway for zero-init check |
| 117 |
union { |
| 118 |
float f; // is the first member of the anon union and must be checked |
| 119 |
int i; |
| 120 |
} |
| 121 |
} |
| 122 |
static assert(!__traits(isZeroInit, S8)); |