Casts and some suggestions to avoid them
bearophile
bearophileHUGS at lycos.com
Tue Apr 8 14:09:00 PDT 2014
H. S. Teoh:
> Which cases don't work?
Example: in a nothrow function. Unless you catch the exception
locally.
To solve this in Bugzilla I have proposed a nothrow function
maybeTo that returns a Nullable!T:
https://d.puremagic.com/issues/show_bug.cgi?id=6840
Also a cast is faster and lighter than to! so in some cases it's
needed.
> This issue will (hopefully?) be addressed when Andrei finalizes
> his allocators, perhaps?
Andrei allocators are very nice, and they help, but I think they
can't replace the C allocation functions in every case.
> I think to! can probably be extended to perform this conversion.
It's not so simple, there are some constraints.
> Using a union here is not a good idea, because the results
> depend on the endianness of the machine! It's better to
> just use (a & 0xFFFF) or (a >> 16) instead.
Right.
> This is inherently unsafe, since it risks silent truncation of
> very large arrays.
In some cases you can assume to not have huge arrays. And you can
even test the hugeness of the length before the cast or inside
the function precondition.
Bye,
bearophile
More information about the Digitalmars-d
mailing list