www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dmd casts but ldc doesn't, and doesn't work in template in dmdm

reply Mike B Johnson <Mikey Ikes.com> writes:
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)
Jun 04
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Jun 04
parent reply Mike B Johnson <Mikey Ikes.com> writes:
On Monday, 5 June 2017 at 00:56:52 UTC, Jonathan M Davis wrote:
 On Monday, June 05, 2017 00:18:04 Mike B Johnson via 
 Digitalmars-d-learn wrote:
 [...]
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
Guid is a struct and I am trying to get the "bytes" of the struct" to get the guid bytes. It is quicker than accessing all the elements one at a time.
Jun 04
next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Mon, Jun 05, 2017 at 01:14:31AM +0000, Mike B Johnson via
Digitalmars-d-learn wrote:
[...]
 Guid is a struct and I am trying to get the "bytes" of the struct" to
 get the guid bytes. It is quicker than accessing all the elements one
 at a time.
union U { typeof(guid) guid; ubyte[guid.sizeof] bytes; } U u; u.guid = guid; // ... do something with u.bytes. T -- Why are you blatanly misspelling "blatant"? -- Branden Robinson
Jun 04
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Jun 04, 2017 at 06:12:42PM -0700, H. S. Teoh wrote:
 On Mon, Jun 05, 2017 at 01:14:31AM +0000, Mike B Johnson via
Digitalmars-d-learn wrote:
 [...]
 Guid is a struct and I am trying to get the "bytes" of the struct" to
 get the guid bytes. It is quicker than accessing all the elements one
 at a time.
union U { typeof(guid) guid; ubyte[guid.sizeof] bytes; } U u; u.guid = guid; // ... do something with u.bytes.
[...] And if you're going to be doing this a lot on many different types, you could ease the typing by declaring a template for it, for example: union AsBytes(T) { T t; ubyte[T.sizeof] bytes; } ubyte[T.sizeof] asBytes(T)(T t) { AsBytes!T u; u.t = t; return u.bytes; } ... struct S { /* stuff */ } S s; auto bytes = s.asBytes; ... /* do stuff with bytes, which is a static array of ubyte */ Note, of course, that this will be system if T contains any pointers. T -- 2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.
Jun 04