postblit, const(T) copy, dealing with structs

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 13 22:31:47 PST 2012


On Tuesday, November 13, 2012 21:40:57 Dan wrote:
> I'm particularly interested in knowing if my handful of casts in
> this code are as safe as I hope.
> https://github.com/patefacio/d-help/blob/master/d-help/opmix/mix.d

Glancing over them, I don't see any major problems. Casting to immutable is 
safe as long as there are no other mutable references to the data, and casting 
away const is safe as long as you don't mutate anything after casting const 
away (though ideally you still wouldn't be casting away const).

The one cast that I'd be a bit concerned about would be casting the floating 
point value in deepHash to its representative bytes. I believe that the cast 
itself is okay, but I don't think that it's necessarily the case that the same 
floating point value will always have the same physical representation. At 
minimum, NaN won't. So, it _might_ work with all normal floating point numbers 
(I'm not well enough versed in the exact layout of IEEE floating point numbers 
to be sure), but it _won't_ work with NaN. At minimum, you'll have to special 
case NaN if you want NaN to always result in the same hash. Of course, if a 
hash table were to use == as part of finding the key (I don't think that the 
built-in AAs currently do though), NaN would be pretty screwed anyway (since 
it would never be found even if it were there 50 times), so I don't know how 
big a deal it is. Better safe than sorry though.

- Jonathan M Davis


More information about the Digitalmars-d mailing list