digitalmars.D.learn - Checking if a structs .init value is zero bits only
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (4/4) Mar 27 2018 Is there a way to check if a struct `S` can be initialized using
- =?UTF-8?Q?Ali_=c3=87ehreli?= (29/32) Mar 27 2018 The following idea should work. One question that I'm not certain about
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/22) Mar 28 2018 Yes, of course, thanks.
- Seb (4/8) Mar 27 2018 Have a look at:
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (4/7) Mar 28 2018 Exactly what I wanted. Thanks!
Is there a way to check if a struct `S` can be initialized using zero bits only, so that we can allocate and initialize an array of `S` in one go using `calloc`? If not, what should such a trait look like?
Mar 27 2018
On 03/27/2018 05:15 PM, Per Nordlöw wrote:Is there a way to check if a struct `S` can be initialized using zero bits only, so that we can allocate and initialize an array of `S` in one go using `calloc`? If not, what should such a trait look like?The following idea should work. One question that I'm not certain about is whether padding bytes inside .init can ever be non-zero in D. I assumed they are always zero. If not, the same idea must be applied recursively to individual members. bool allZeros(T)() { // Yes, this can be implemented as a range algorithm. :) T t; foreach (b; (cast(ubyte*)&t)[0..T.sizeof]) { if (b) { return false; } } return true; } unittest { static struct A { int i; long l; } static struct B { double d; } assert(allZeros!A); assert(!allZeros!B); } void main() { } Ali
Mar 27 2018
On Wednesday, 28 March 2018 at 00:50:31 UTC, Ali Çehreli wrote:On 03/27/2018 05:15 PM, Per Nordlöw wrote:Yes, of course, thanks. But my goal is (always) to have it done at compile-time.Is there a way to check if a struct `S` can be initialized using zero bits only, so that we can allocate and initialize an array of `S` in one go using `calloc`? If not, what should such a trait look like?The following idea should work. One question that I'm not certain about is whether padding bytes inside .init can ever be non-zero in D. I assumed they are always zero. If not, the same idea must be applied recursively to individual members. bool allZeros(T)() { // Yes, this can be implemented as a range algorithm. :) T t; foreach (b; (cast(ubyte*)&t)[0..T.sizeof]) { if (b) { return false; } } return true; }
Mar 28 2018
On Wednesday, 28 March 2018 at 00:15:34 UTC, Per Nordlöw wrote:Is there a way to check if a struct `S` can be initialized using zero bits only, so that we can allocate and initialize an array of `S` in one go using `calloc`? If not, what should such a trait look like?Have a look at: https://github.com/dlang/phobos/pull/6024 (review/feedbackon this PR is welcome!)
Mar 27 2018
On Wednesday, 28 March 2018 at 01:39:40 UTC, Seb wrote:Have a look at: https://github.com/dlang/phobos/pull/6024 (review/feedbackon this PR is welcome!)Exactly what I wanted. Thanks! In use here https://github.com/nordlow/phobos-next/blob/41b9e0dcfbb4eed6b2ee52d0465425556f14c00f/src/open_hashmap_or_hashset.d#L242
Mar 28 2018