dmd casts but ldc doesn't, and doesn't work in template in dmdm

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 4 17:56:52 PDT 2017


On Monday, June 05, 2017 00:18:04 Mike B Johnson via Digitalmars-d-learn 
wrote:
> The following line is causing some problems.
>
>
>      auto bytes = cast(byte[16])guid;
>
>
> compiles fine in dmd but ldc says it can't convert... also, it
> doens't work in ctfe/template either. (I'm not sure if ctfe is
> kicking in or not though, but definitely doesn't work in a
> template)

I assume that guid is a string or array of byte or somesuch? If it's an
array of byte or char, then you can probably do

auto bytes = cast(byte[16])guid[0 .. 16];

I doubt that it will work with CTFE though, because this is basically a
reinterpret cast, and CTFE gets picky about casts like that. If you need to
do something like that in CTFE, you'll probably need to use if(__ctfe) to
add a branch that does this it in a for loop or something without any casts.

Also, you probably want ubyte, not byte. byte is signed, so it really only
makes sense to use it as an integral value that holds [-128, 128] rather
than for an actual byte value.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list