casting array literals doesn't work as stated in the docs

Steven Schveighoffer schveiguy at yahoo.com
Sat Jan 2 20:09:40 PST 2010


On Sat, 02 Jan 2010 22:41:05 -0500, Denis Koroskin <2korden at gmail.com>  
wrote:


> Array casting is something I avoid as a plague. It is highly  
> inconsistent: in some cases, it does bitwise casting (kind of a  
> reinterpret_cast, changing size of an array), in others - in creates  
> per-element copy of an array, thus doing at runtime something I'd like  
> to be done at compile-time.
>
> Maybe it's just me, but I wouldn't recommend anyone using array casting  
> (other that T[] -> void[]).

It's very consistent.  It never does a per-element copy at runtime.   
Essentially, doing this:

cast(byte[])[a, b, c]

is equivalent to doing this:

[cast(byte)a, cast(byte)b, cast(byte)c]

the rules are:

if you cast an array literal, then it reinterprets each element as if each  
element were cast to that type.

otherwise, it's a reinterpret_cast as you say.

-Steve



More information about the Digitalmars-d mailing list